/*Contains javascript libraries used on the website*/

/*
* vertical news ticker
* Tadas Juozapaitis ( kasp3rito@gmail.com )
* http://www.jugbit.com/jquery-vticker-vertical-news-ticker/
*/
(function($){
$.fn.vTicker = function(options) {
	var defaults = {
		speed: 700,
		pause: 4000,
		showItems: 3,
		animation: '',
		mousePause: true,
		isPaused: false,
		direction: 'up',
		height: 0
	};

	var options = $.extend(defaults, options);

	moveUp = function(obj2, height, options){
		if(options.isPaused)
			return;
		
		var obj = obj2.children('ul');
		
    	var clone = obj.children('li:first').clone(true);
		
		if(options.height > 0)
		{
			height = obj.children('li:first').height();
		}		
		
    	obj.animate({top: '-=' + height + 'px'}, options.speed, function() {
        	$(this).children('li:first').remove();
        	$(this).css('top', '0px');
        });
		
		if(options.animation == 'fade')
		{
			obj.children('li:first').fadeOut(options.speed);
			if(options.height == 0)
			{
			obj.children('li:eq(' + options.showItems + ')').hide().fadeIn(options.speed).show();
			}
		}

    	clone.appendTo(obj);
	};
	
	moveDown = function(obj2, height, options){
		if(options.isPaused)
			return;
		
		var obj = obj2.children('ul');
		
    	var clone = obj.children('li:last').clone(true);
		
		if(options.height > 0)
		{
			height = obj.children('li:first').height();
		}
		
		obj.css('top', '-' + height + 'px')
			.prepend(clone);
			
    	obj.animate({top: 0}, options.speed, function() {
        	$(this).children('li:last').remove();
        });
		
		if(options.animation == 'fade')
		{
			if(options.height == 0)
			{
				obj.children('li:eq(' + options.showItems + ')').fadeOut(options.speed);
			}
			obj.children('li:first').hide().fadeIn(options.speed).show();
		}
	};
	
	return this.each(function() {
		var obj = $(this);
		var maxHeight = 0;

		obj.css({overflow: 'hidden', position: 'relative'})
			.children('ul').css({position: 'absolute', margin: 0, padding: 0})
			.children('li').css({margin: 0, padding: 0});

		if(options.height == 0)
		{
			obj.children('ul').children('li').each(function(){
				if($(this).height() > maxHeight)
				{
					maxHeight = $(this).height();
				}
			});

			obj.children('ul').children('li').each(function(){
				$(this).height(maxHeight);
			});

			obj.height(maxHeight * options.showItems);
		}
		else
		{
			obj.height(options.height);
		}
		
    	var interval = setInterval(function(){ 
			if(options.direction == 'up')
			{ 
				moveUp(obj, maxHeight, options); 
			}
			else
			{ 
				moveDown(obj, maxHeight, options); 
			} 
		}, options.pause);
		
		if(options.mousePause)
		{
			obj.bind("mouseenter",function(){
				options.isPaused = true;
			}).bind("mouseleave",function(){
				options.isPaused = false;
			});
		}
	});
};
})(jQuery);


/* jquery events*/

$(document).ready(function()
	{	
	
	
		/* LOGIN */
		$("#login_form").submit(function() {
				var unameval = $("#username").val();
				var pwordval = $("#password").val();
				$.post("library/ajax/login.php", { username: unameval,
				password: pwordval }, function(data) {
						
					if(data == 1)
					{
						location.reload();
					}
					else if(data == 0)
					{
						alert("Login Failed");	
					}
				});
				return false;
			});		
		
		$("#logout-button").click(function() {
				document.location.href = "logout.php";
			});	
		
		
		/* FORGOT PASSWORD */
		$("#forgot_password_form").submit(function() {
				var emailval = $("#email_forgot").val();
				$.post("library/ajax/retrievepass.php", { email: emailval }, function(data) {
						
					if(data == 1)
					{
						alert('Your Account Information has been sent to your email address.');
						location.reload();
					}
					else if(data == 0)
					{
						alert("Email does not exist.");	
					}
					else if(data == 2)
					{
						alert("Please enter your registered email address.");	
					}
				});
				return false;
			});		
		
		$("#email_forgot").focus(function () {
			 $(this).val("").css({color: 'black'});
		});
		
		
		
		/*REGISTRATION*/
		
		$("#app_username").blur(function()
			{
				var username = jQuery.trim($("#app_username").val());
				$("#username_message").text('Validating...').fadeIn(1000);
				
				if( username == "" ) 

					$("#username_message").text('사용자 ID가 필요합니다.').fadeIn(1000); 

				else if( username.length < 6 ) 

					$("#username_message").text('Username must have at least 6 characters.').fadeIn(1000);

				else if( username.length > 15 ) 

					$("#username_message").text('Username must not exceed 15 characters.').fadeIn(1000);

				else

				{

					$.post("library/ajax/registration_field_checker.php",{ val:username, type:'xxyxx', rand:Math.random() } ,function(data)

					{

						$("#username_message").html( data ).fadeIn(1000);

					});

				}

			});

			

			$("#app_password").blur(function()

			{

				var password = $("#app_password").val();

				if( password == "" ) 

					$("#password_message").text('Password가 필요합니다.').fadeIn(1000);

				else if( password.length < 6 ) 

					$("#password_message").text('Password must have at least 6 characters.').fadeIn(1000);

				else if( password.length > 32 ) 

					$("#password_message").text('Password must not exceed 32 characters.').fadeIn(1000);

				else

					$("#password_message").text('').fadeIn(1000);

			});

			

			$("#app_conpass").blur(function()

			{

				if( $("#app_password").val() != $("#app_conpass").val()) 

					$("#conpass_message").text('Please verify your password again.').fadeIn(1000);

				else

					$("#conpass_message").text('').fadeIn(1000);

			});
		 
		 	
			
			$("#app_email").blur(function()

			{

				var email = jQuery.trim( $("#app_email").val() );

				$("#email_message").text('Validating...').fadeIn(1000);

				if( email == "" ) 

					$("#email_message").text('이메일 주소가 필요합니다.').fadeIn(1000);

				else if( !(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/).test(email) )

					$("#email_message").text('Invalid email address.').fadeIn(1000);

				else

				{

					$.post("library/ajax/registration_field_checker.php",{ val:email, type:'yyxyy', rand:Math.random() } ,function(data)

					{

						$("#email_message").html( data ).fadeIn(1000);

					});

				}

			}); 
			
	
	
		$(".inputSubmit").mousedown(function(){
			  $(this).css({backgroundPosition: 'bottom'});
		}).mouseup(function(){
			  $(this).css({backgroundPosition: 'top'});
		});			
			
			
			
		
		/* Language Events*/
		
		$(".kr-lang").mousedown(function(){
			  $(this).css({backgroundPosition: '-259px -112px'});
		}).mouseup(function(){
			  $(this).css({backgroundPosition: '-259px -132px'});
			  $(".en-lang").css({backgroundPosition: '-206px -132px'});
		});
		
		/* Login Events*/
		
		$(".login-button").mousedown(function(){
			  $(this).css({backgroundPosition: '-319px -89px'});
		}).mouseup(function(){
			  $(this).css({backgroundPosition: '-319px -65px'});
		});
		
		/* Newsboard Events*/
		
		$(".board-button").mousedown(function(){
			  $(this).css({backgroundPosition: '-69px -22px'});
			  $(".news-button").css({backgroundPosition: '-0px -22px'});
			  $("#news_content").hide();
			  $("#board_content").css({visibility: 'visible'});
		});
		
		$(".news-button").mousedown(function(){
			  $(this).css({backgroundPosition: '0px 0px'});
			  $(".board-button").css({backgroundPosition: '-69px -0px'});
			  $("#board_content").css({visibility: 'hidden'});
			  $("#news_content").show();
		});
		
		$('#links').vTicker({
		   speed: 500,
		   pause: 2800,
		   showItems: 1,
		   mousePause: true,
		   height: 0,
		   direction: 'up'
		});
		
		$('#read-about').vTicker({
		   speed: 500,
		   pause: 3000,
		   showItems: 1,
		   mousePause: true,
		   height: 0,
		   direction: 'up'
		});
		
		/*project slide effect*/
		
		lastBlock = $("#a1");
		maxWidth = 280;
		minWidth = 35;	
	
		$("ul li div").click(
		  function(){
			$(lastBlock).animate({width: minWidth+"px"}, { queue:false, duration:400 });
			$(this).animate({width: maxWidth+"px"}, { queue:false, duration:400});
			lastBlock = this;
		  }
		);
		
		/*Clients Home Event*/
		
		$("div#makeMeScrollable").smoothDivScroll({ autoScroll: "onstart", autoScrollDirection: "endlessloopright", autoScrollStep: 1, autoScrollInterval: 35, scrollStep: 15, scrollInterval: 50, startAtElementId: "startAtMe", visibleHotSpots: "always"});
		
		
		$("table.left-links tr").hover(
		  function () {
			 $(this).css({backgroundColor: '#FFFF99'});
		  }, 
		  function () {
			 $(this).css({backgroundColor: '#FFF'});
		  }
		);
		
		$("table.left-links tr td").click(
		  function(){
			  var link = $(this).find("a").attr("href")
			  document.location.href = link;
		  }
		);
		
		
		
		/* Business Change Buttons*/
		
		$(".proposed-notselected").mousedown(function(){
			  $(this).css({backgroundPosition: '-129px -28px'});
		}).mouseup(function(){
			  $(this).css({backgroundPosition: '-129px 0px'});
		});
		
		$(".completed-notselected").mousedown(function(){
			  $(this).css({backgroundPosition: '0px -28px'});
		}).mouseup(function(){
			  $(this).css({backgroundPosition: '0px 0px'});
		});
		
	
	
		/*Left Menu*/
		
		$("table.left_menu tr td").hover(
		  function () {
			 if($(this).hasClass('active'))
			 {
				$(this).css({backgroundPosition: '0px 0px'}); 
			 }
			 else
			 {
				$(this).css({backgroundPosition: '0px -56px'}); 
			 }
			 
		  }, 
		  function () {
			 if($(this).hasClass('active'))
			 {
				$(this).css({backgroundPosition: '0px 0px'}); 
			 }
			 else
			 {
				$(this).css({backgroundPosition: '0px -28px'});
			 }
			 
		  }
		);
		
		
		$("table.left_menu tr td").click(
		  function(){
			  var link = $(this).find("a").attr("href")
			  document.location.href = link;
		  }
		);
		
		
		//add news button
		$(".add_news_button").mousedown(function(){
			  $(this).css({backgroundPosition: 'bottom'});
		}).mouseup(function(){
			  $(this).css({backgroundPosition: 'top'});
		});
		
		
		/*Add news comment*/
		$("#news_comment_form").submit(function() {
				var ucomment = $("#cmnt").val();
				var uid = $("#userid").val();
				var nid = $("#newsid").val();
				var uphoto = $("#userphoto").val();
				var name = $("#uname").val();
				var date = $("#cdate").val();
				var cTotal =  $("#commenTotal").val();
				parseInt(cTotal);
				cTotal++;
				
				
				ucomment = ucomment.replace(/\n\r?/g, '<br />');
				
				if(!ucomment){
					alert('Please enter a comment.');
				}
				else
				{
					$.post("library/ajax/post_news_comment.php", { comment: ucomment , userid: uid, newsid: nid }, function(data) {
						if(data > 0)
						{
							
							$("#total_news_comments").html(cTotal);
							$("#commenTotal").val(cTotal);
							
							$("div.comment_wrap2").prepend('<div class="comment_blocks first"><img src="resources/img/mem_photos/thumbs/'+uphoto+'" width="60" height="60" /><div class="comment_content_container"><p class="name"><span class="bold namespan">'+name+'</span>&nbsp;- &nbsp;<span class="date">'+date+'</span></p><p class="comment">'+ucomment+'</p></div> <a href="javascript:void(0);" title="Delete" class="deletecomment" onclick="delete_confirm(this,'+data+')">Delete</a></div>').children(':first').hide().fadeIn("slow").fadeIn(function(){this.style.removeAttribute("filter");});
							$("#cmnt").val("");
							
						}
						else if(data == 0)
						{
							alert("Something went wrong. Please try again.");	
						}
					});
				}
				return false;
				
			});		
		
		/* This code is executed after the DOM has been completely loaded */
	
	var totWidth=0;
	var positions = new Array();
	
	$('#slides .slide').each(function(i){
		
		/* Traverse through all the slides and store their accumulative widths in totWidth */
		
		positions[i]= totWidth;
		totWidth += $(this).width();
		
		/* The positions array contains each slide's commulutative offset from the left part of the container */
		
		if(!$(this).width())
		{
			alert("Please, fill in width & height for all your images!");
			return false;
		}
		
	});
	
	$('#slides').width(totWidth);

	/* Change the cotnainer div's width to the exact width of all the slides combined */

	$('#pg_menu ul li a').click(function(e,keepScroll){

			/* On a thumbnail click */

			$('li.menuItem').removeClass('act').addClass('inact');
			$(this).parent().addClass('act');
			
			var pos = $(this).parent().prevAll('.menuItem').length;
			
			$('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
			/* Start the sliding animation */
			
			e.preventDefault();
			/* Prevent the default action of the link */
			
			
			// Stopping the auto-advance if an icon has been clicked:
			if(!keepScroll) clearInterval(itvl);
	});
	
	$('#pg_menu ul li.menuItem:first').addClass('act').siblings().addClass('inact');
	/* On page load, mark the first thumbnail as active */
	
	
	
	/*****
	 *
	 *	Enabling auto-advance.
	 *
	 ****/
	 
	var current=1;
	function autoAdvance()
	{
		if(current==-1) return false;
		
		$('#pg_menu ul li a').eq(current%$('#pg_menu ul li a').length).trigger('click',[true]);	// [true] will be passed as the keepScroll parameter of the click function on line 28
		current++;
	}

	// The number of seconds that the slider will auto-advance in:
	
	var changeEvery = 10;

	var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);

	/* End of customizations */
		
		
		
	}); //end of document.ready


function redirect_this(where)
{
	var loc = $(where).val();
	document.location.href = loc+'.php';	
	
}
