/** main jquery javascript **/
function show(id){
	if (document.getElementById){
		obj = document.getElementById(id);
		if (obj.style.display == "none"){
			obj.style.display = "";
		}
		else{
			$(obj).toggle(500);
		}
	}
}

function show_hide(div, type, element){
	var divs = div.split(',');
	if(type == 1){
		if(element == 1){
			for(i = 0; i < divs.length; i++){				
				if( $("#" + divs[i]).css("display") == "none" )
					$("#" + divs[i]).fadeIn("slow");
			}
		}
		else{
			for(i = 0; i < divs.length; i++){
				$("#" + divs[i]).fadeOut("normal");
			}
		}
	}
	else{
		if( $("#" + divs).css("display") == "none" )
			$("#" + divs).fadeIn("slow");
		else
			$("#" + divs).fadeOut("normal");
	}
}

function save(url,form,msg_div,loader) {
	
	$("#"+msg_div).hide();
	$('#'+loader).show();
	
	$.ajax({
		type: "POST",
		url: url,
		data: $('#'+form).serialize(),
		success: function(output){						
			$('#'+loader).hide();
			$("#"+msg_div).show();
			$("#"+msg_div).html(output);
		}
	});
}

function getData(page,loader_div,response_div) {

	var url = page;
	//$("#"+response_div).hide();
	$('#'+loader_div).show();
	
	$.ajax({
		url: url,
		type: "POST",
		data: ({flag : 1}),
		success: function(output){
			$('#'+loader_div).hide();
			$("#"+response_div).html(output);
			//$("#"+response_div).show();
			return false;
		}
	})
}

function verify_email(email,user,uID,act_code) {
	
	var url = 'ajaxifiers/verify_email?email='+email+'&uID='+uID+'&user='+user+'&act_code='+act_code;
	$.ajax({
		url: url,
		type: "POST",
		data: ({flag : 1}),
		success: function(output){
			if(output == 'success')
				alert("Email sent! Please check your email address.\n\nEmail Address used: "+email);
			else
				alert("Error sending email to "+email);
			return false;
		}
	})
}

function limitChars(textid, limit, infodiv){
	var text = $('#'+textid).val();	
	var textlength = text.length;
	if(textlength > limit){
		$('#' + infodiv).html('You cannot write more than '+limit+' characters!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else{
		$('#' + infodiv).html((limit - textlength) +' character/s left.');
		return true;
	}
}

jQuery(document).ready(function($) { 
	
	$("#typewriter").scrambledWriter();
	$("#typewriter2").typewriter();
	$("#typewriter3").scrambledWriter();
	
	$("ul.topnav li span").hover(function() { //When trigger is clicked...
		
		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

		$(this).parent().hover(function() {
		}, function(){	
			$(this).parent().find("ul.subnav").slideUp('normal'); //When the mouse hovers out of the subnav, move it back up
		});

	//Following events are applied to the trigger (Hover events for the trigger)
	}).hover(function() { 
		$(this).addClass("subhover"); //On hover over, add class "subhover"
	});
	
	$("a[rel]").overlay({

		// custom top position
		top: 10,
		// some expose tweaks suitable for facebox-looking dialogs
		expose: {
			// you might also consider a "transparent" color for the mask
			color: '#000',

			// load mask a little faster
			loadSpeed: 200,

			// highly transparent
			opacity: 0.5
		},
		// disable this for modal dialog-type of overlays
		closeOnClick: true,
		// we want to use the programming API
		api: true,
		onBeforeLoad: function() {
			$(".popup_content").html("<div style='text-align: center; margin: 50px;'><img src='public/images/common/loader4.gif' alt='loader' /></div>");
			// grab wrapper element inside content
			var wrap = this.getContent().find(".popup_content");
			// load the page specified in the trigger
			wrap.load(this.getTrigger().attr("href"));
		}
	});
	
	$(".follow_img").fadeTo("slow", 1.0); // This sets the opacity of the thumbs to fade down to 100% when the page loads
		$(".follow_img").hover(function(){
		$(this).fadeTo("slow", 0.3); // This should set the opacity to 30% on hover
		},function(){
		$(this).fadeTo("slow", 1.0); // This should set the opacity back to 100% on mouseout
	});
	
	$('#left_content marquee').marquee('pointer').mouseover(function () {
		$(this).trigger('stop');
	}).mouseout(function () {
		$(this).trigger('start');
	})
	
	$("a[sharewith]").click(function(ev){
	   var rel = $(this).attr("sharewith");
	   var url = encodeURIComponent(self.location.href);
	   var title = encodeURIComponent($("title:first").html());
	   rel = rel.replace("{url}",url);
	   rel = rel.replace("{title}",title);
	   window.open(rel);
	   return false;
	});
	
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("current"); //Remove any "active" class
		$(this).addClass("current"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active content
		return false;
	});
	
	$('#comment_post').keyup(function(){
		limitChars('comment_post', 500, 'charlimit');
	});
	
	$('#shoutout').keyup(function(){
		limitChars('shoutout', 140, 'shoutout_limit');
	});
	
	$("#comment_form").submit(function(){
				
		$('#comment_loader').show();
		$.ajax({
			type: "POST",
			url: "ajaxifiers/save_comment.php",
			data: $(this).serialize(),
			success: function(output){
			
				$('#comment_loader').hide();
				$("#comment_msg").show();
				
				if(output == 'success'){
					$("#comment_msg").html('<div class="success_message">Your comment has been added.</div>');
					var page = $("#page").val();
					var page_id = $("#page_id").val();
					var total = parseInt( $("#total_comments").val() ) + 1;
					$("#total_comments_display").html(total);
					$("#total_comments").val(total);
					
					var params = 'type='+page+'&page_id='+page_id;
					var limit = 5;
					getData('ajaxifiers/view_comments.php?'+params, 'comments_loader', 'comments_view'); 
				}
				else{
					$("#comment_msg").html(output);
				}
			}
		});
		return false;
		
	});
	
	$("#shoutoutbutton").click(function(){
				
		$('#shoutout_loader').show();
		var shoutout = $.trim( $('#shoutout').val() );
		
		if(shoutout == ''){
			$('#shoutout_loader').hide();
			alert("Please post your shoutout");
		}
		else{
			
			$.ajax({
				type: "POST",
				url: "ajaxifiers/save_shoutout.php",
				data: ({shoutout : shoutout}),
				success: function(msg){
					$('#shoutout_loader').hide();
					if(msg == 'error') {
						alert("Error processing data. Please try again.");
					}
					else {
						$('#shoutout_div').hide();
						$('#shoutout_main').html('<i><strong>My Shoutout:</strong></i><br /><i>'+msg+'</i><br /><span style="float: right"><a href="javascript: void(0);" onclick="show(\'shoutout_div\');">Change</a></span>');
					}
				}
			});

		}
		return false;
	});
	
	$(document).pngFix();

	var first = 0;
	var speed = 700;
	var pause = 3500;

	function removeFirst(){
		first = $('ul#listticker li:first').html();
		$('ul#listticker li:first')
		.animate({opacity: 0}, speed)
		.fadeOut('slow', function() {$(this).remove();});
		addLast(first);
	}

	function addLast(first){
		last = '<li style="display:none">'+first+'</li>';
		$('ul#listticker').append(last)
		$('ul#listticker li:last')
		.animate({opacity: 1}, speed)
		.fadeIn('slow')
	}
	interval = setInterval(removeFirst, pause);
	
});
/** end **/
