// JavaScript Document


var current_menu = "";
var li = '';
function show_menu(menu)
{
	if(current_menu!=menu)
		hide_menu()
	$("#"+menu).fadeIn(200)
	current_menu = menu;	
	$("."+menu).addClass("selected");
}

function hide_menu()
{
	if(current_menu!='')
	{
		$("#"+current_menu).hide(); 
		$("."+current_menu).removeClass("selected");
		return true;
	}
}

var current_host = "";
function show_host(host)
{
	if(current_host!=host)
	hide_host()
	$("#"+host).fadeIn(300)
	current_host = host;	
	$("."+host).addClass("selected");
}

function hide_host()
{
	if(current_host!='')
	{
		$("#"+current_host).hide() 
		$("."+current_host).removeClass("selected");
		return true;
	}
}


function default_value(text,Obj)
{
	if($(Obj).val()=='')
		$(Obj).attr('value',text)
}

function toggle_button(id)
{
	if($(id).css("background-position") !='0px -18px')
	{
		$(id).css("background-position","0px -18px");
		$(id).css("color","#09c");
	}else{
		$(id).css("background-position","0px 0px");
		$(id).css("color","#333");
	}	
}

function toggle_social() {
	
	$(".hide_social").click(					 
		function() {
			$('.hide_social').css( { 'display' : 'none' });
			$('.show_social').css( { 'display' : 'block' });
			$('#social_icons').stop().animate( { right: "-37px" }, 600);
			$.cookie("status","hidden");
	});
	
	$(".show_social").click(					 
		function() {
			$('.hide_social').css( { 'display' : 'block' });
			$('.show_social').css( { 'display' : 'none' });
			$('#social_icons').stop().animate( { right: "0" }, 600,"easeOutBounce");
			$.cookie("status","shown");
	});	
	
	var state = $.cookie('status');
	
	if( state == 'hidden') {
		$('.hide_social').css( { 'display' : 'none' });
		$('.show_social').css( { 'display' : 'block' });
		$('#social_icons').css( {'right' : '-37px'} )
	}
}

function big_img(src)
{
	$("#big_thumb").attr('src',src);
}

function check_download(id)
{
	var page=baseurl+'/check_download.php';
	$.post(page,
		   {
			   id : id
		   },
		   function(data)
		   {
				if(data)
				{
					window.location = data.dlink;
				}else
				{
					// load the contact form using ajax
					$.get(baseurl+"js/ajax/contact.php?id="+id, function(data){
					// create a modal dialog with the data
					$(data).modal({
					closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
					position: ["15%",],
					overlayId: 'contact-overlay',
					containerId: 'contact-container',
					onOpen: contact.open,
					onShow: contact.show,
					onClose: contact.close
					});
					});
					
					
					// preload images
					var img = ['cancel.png', 'form_bottom.gif', 'form_top.gif', 'loading.gif', 'send.png'];
					$(img).each(function () {
					var i = new Image();
						i.src = imageurl+'contact/' + this;
					});
				}
		   },
		   "json"
		   );
}






function plugin_rating(pid,rating)
{
	var page=baseurl+'/ajax.php';
	$.post(page,{mode :"plugin_rating",pid:pid,rating:rating},
		   function(data)
		   {
			   $("#thumbs_container a").removeAttr("onclick");
			   $("#rating_result").html(data);
			   alert("Thanks for rating this plugin!!");
		   },
		   "text");
}

function rate_theme(tid,rate)
{
	var page=baseurl+"ajax.php";
	
	$.post(page,
			{
				mode : "theme_rating",
				tid  : tid,
				rate : rate	
			},
			function(data)
			{
				$("#rate_container a").removeAttr('onclick');
				$("#current_rate").html(data).hide().fadeIn(350);
			},"text");
}

function parseTweets(tweets)
{
	var tweetHTML = [];
	for(var i=0;i<tweets.length;i++)
	{
		var username = tweets[i].user.screen_name;
		var status = tweets[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
		  return '<a href="'+url+'" target="_blank">'+url+'</a>';
		}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
		  return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
		});
		var iteration = i + 1;
		tweetHTML.push('<li class="tweetItem clearfix"><div class="inlineBlock tweetNumber">'+iteration+'</div><div class="inlineBlock tweetContent"><h2 class="heading yanone medBig color-333" style="margin-bottom:0px;">'+status+'</h2><span class="color-999 small"><a href="http://twitter.com/'+username+'/statuses/'+tweets[i].id_str+'" target="_blank" class="color-999">'+relative_time(tweets[i].created_at)+'</a></span></div></li> ');	
	}
	tweetHTML.push('<div class="textCenter yanone block reallyBig"><a href="http://twitter.com/'+username+'" target="_blank" class="block noDecorationHover tweetReadMore padding5-0 ">Read More</a></div>');
	var innerHTML = tweetHTML.join('');
	
	return innerHTML;
}

/**
 * Function used to center an element in center
 * of parent element. Usage
 * <div class="relative">
 * 	<div id="@element" style="visibilty:hidden">
 * 		Content To Center
 * 	</div>
 * </div>
 */
function makeElementDeadCenter(element)
{
	
	if($("#"+element))
	{
		var el = document.getElementById(element), 
				obj = $("#"+element), parentDIV = obj.parent();
						
			var parentHeight = parentDIV.outerHeight();
			var topPosition = parentHeight / 2; 
			var topMargin = obj.outerHeight() / 2;		
			topPosition = topPosition - topMargin;
			if(parentDIV.css('position') != 'relative')
				parentDIV.css('position','relative');
				
			obj.css({
				position: "relative",
				top : topPosition+"px",	
				visibility : "visible",
				opacity : 0
			}).animate({
				opacity : 1
			},500)		
	}
}
