/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup( target ){
	if(popupStatus==1){
		disablePopup(target);
	}

	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").addClass("background_opacity");
		$("#backgroundPopup").fadeIn(200);
		$("#" + target).fadeIn(200);
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup( target ){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut(200);
		$("#backgroundPopup").removeClass("background_opacity");
		$("#" + target).fadeOut(200, function(){
			$('#popupContent').html("");
		});
		popupStatus = 0;
	}
}

//centering popup
function centerPopup( target ){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#" + target).height();
	var popupWidth = $("#" + target).width();
	//centering
	$("#" + target).css({
		"position": "absolute",
		"top": 100,
		"left": windowWidth/2-popupWidth/2
	});
	
	//setTimeout('centerPopupButtons(\'' + target + '\');', 5000); 
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

function centerPopupButtons( target ){
	var windowWidth = document.documentElement.clientWidth;
	var popupWidth = $("#" + target).width();
	
	$(".popupBoxButton").css({
		"position": "fixed",
		"left": windowWidth/2 + popupWidth/2 + 100
	});
}

// get article data
function getArticle( article_id ){

	$('#popupContent').html(" ");

	$.ajax({
	  url: '/tbd/loadarticle/' + article_id,
	  beforeSend: function(){
		$('#popupContent').html("<div id=\"popupLoader\">Loading Article</div>");
		$("#popupContent").hide();
		$("#popupContent").fadeIn("slow");
	  },
	  success: function(data) {
		$('#popupContent').html(data);
	  }
	});
}

var scrolling = false;

function scrollWindow( destination ){
	if(scrolling == false){
		scrolling = true;
		$('html,body').animate({
			scrollTop: $('#' + destination).offset().top
		}, 400, "linear", function(){ scrolling = false; } 
		);
	}
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){  
	
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupBoxClose").click(function(){
		window.location = $('#save_url').attr('data');
		disablePopup('popupBox');
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		window.location = $('#save_url').attr('data');
		disablePopup('popupBox');
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			window.location = $('#save_url').attr('data');
			disablePopup('popupBox');
		}
	});

	checkAnchor();
	setInterval("checkAnchor()", 300); 

});
