$(document).ready(function(){
	/* The following code is executed once the DOM is loaded */
	$('.sponsorFlip').bind("click",function(){
		// $(this) point to the clicked .sponsorFlip element (caching it in elem for speed):
		var elem = $(this);
		
		// data('flipped') is a flag we set when we flip the element:
		if(elem.data('flipped'))
		{
			// If the element has already been flipped, use the revertFlip method
			// defined by the plug-in to revert to the default state automatically:
			elem.revertFlip();
			// Unsetting the flag:
			elem.data('flipped',false)
		}
		else
		{			
			// Using the flip method defined by the plugin:
			elem.flip({
				direction:'lr',
				speed: 350,
				onBefore: function(){
					// Insert the contents of the .sponsorData div (hidden
					// from view with display:none) into the clicked
					// .sponsorFlip div before the flipping animation starts:					
					elem.html(elem.siblings('.sponsorData').html());
				}
			});
			// Setting the flag:
			elem.data('flipped',true);
		}
	});

	/*-----------------------------------------*/
	var w_height = $(window).height();
	var w_width = $(window).width();

	$("#open").click(
		function (){
			//$(".package-hidden").css("display", "block");
			//$(".app-display-bg").css("width", w_width);
			//$(".app-display-bg").css("height", w_height);
			$(".app-display-bg").css("display", "block");
			$(".app-display-x").fadeIn(500);
			$(".app-display-content").fadeIn(500);
			$app_name = $(this).attr("name");
			/*
			$.ajax({
				type: 'POST',
				url: 'include/app_display.php',
				data: {app_name:$app_name},
				dataType: 'html',
				success: function(data) {
					$('.app-display-content').html(data);
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					$('#.app-display-content').html(textStatus);
				}
			});
			*/
		}
	);
	
	$(".app-display-bg").click(hideAppDisplay);
	$(".app-display-x").click(hideAppDisplay);
	$(".app-display-content .app-info .app-screenshot .screenshot-img").hover(
		function() {
			alert(1);
			showBigImage("");
		},
		function() {
			hideBigImage();
		}
	);
	
});

function hideAppDisplay() {
	$(".package-hidden").css("display", "none");
}

function showAppDisplay(app_name) {
	$(".app-display-bg").css("display", "block");
	$(".app-display-x").fadeIn(500);
	$(".app-display-content").fadeIn(500);
	
	$.ajax({
		type: 'POST',
		url: '/index.php/page/page/view/app-display',
		data: {app_name: app_name},
		dataType: 'html',
		success: function(data) {			
			$('.app-display-content').html(data);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			$('.app-display-content').html(textStatus);
		}
	});
}

function showBigImage(image) {
	$("#big-img-panel img").attr("src", image);
	$("#big-img-panel").css("display", "block");
	var h = $("#big-img-panel").height();
	var w = $("#big-img-panel").width();
	$("#big-img-panel").css("margin-top", -(h/2) + 'px');
	$("#big-img-panel").css("margin-left", -(w/2) + 'px');
}

function hideBigImage() {
	$("#big-img-panel").css("display", "none");
}
//--------------------------------------------------------------------
function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

function loadCssFile(filename) {
	var fileref=document.createElement("link");
	fileref.setAttribute("rel", "stylesheet");
	fileref.setAttribute("type", "text/css");
	fileref.setAttribute("href", "/themes/clear_style/css/" + filename);

	if (typeof fileref!="undefined")
		document.getElementsByTagName("head")[0].appendChild(fileref);
}

function checkVersion()
{
	var msg = "You're not using Internet Explorer.";
	var ver = getInternetExplorerVersion();

	if ( ver > -1 ) {
		if ( ver >= 6.0) { 
			return;
		}
		else {
			msg = "You should upgrade your copy of Internet Explorer.";
			alert( msg );
		}
	}
}

checkVersion(); // check version of IE

//--------------------------------------------------------------------
var cur_container;

function resetHref() {
	if (cur_container == "institution") {
		$("#menu #Mcommunication a").attr("href", "javascript:updateMenu('institution', 'communication')");
		$("#menu #Mcontent a").attr("href", "javascript:updateMenu('institution','content')");
		$("#menu #Mprintmedia a").attr("href", "javascript:updateMenu('institution','printmedia')");
		$("#menu #Mpartners a").attr("href", "javascript:updateMenu('institution','partners')");
		$("#menu #Mcontact a").attr("href", "javascript:updateMenu('institution','contact')");
	} else if (cur_container == "advertiser"){
		$("#menu #Monline a").attr("href", "javascript:updateMenu('advertiser','online')");
		$("#menu #Moffline a").attr("href", "javascript:updateMenu('advertiser','offline')");
		$("#menu #Mpartners a").attr("href", "javascript:updateMenu('advertiser','partners')");
		$("#menu #Mcontact a").attr("href", "javascript:updateMenu('advertiser','contact')");
	}
}

function updateMenu(container, id) {
	$(".book_wrapper").css("display","none");
	$("#clientpage").css("display","none");
	
	$("#menu .menutitle").css("background", 'none');
	$("#menu .menutitle a").css("color", "#143961");
	$("#menu .jsmenu a").attr("href", "#");

	if (id != 'home') {
		$("#menu #M" + id).css("background-color", '#143961');
		$("#menu #M" + id + " a").css("color", "white");
	}	
	ChangeContent(container, id);
}

function ChangeContent(container, ins_des) {
	
	var frame = ".content-page #container #frame";
	var ins_current = $(".content-page #container #main #curPage").html();
	
	var ins_old = ".content-page #container #main #" + ins_current;
	var ins_new = ".content-page #container #main #" + ins_des;
	cur_container = container;

	if($(frame)){
		$(frame).animate(
			{width: "170px"}, 
			1000, 
			function(){
				if(ins_des == 'partners')
							$("#clientpage").css("display","block");
				$(frame).animate(
					{width: "700px"}, 
					1000,
					function (){
						resetHref();						
					}
				);
			}
		);
	}
		
	if($(ins_old)){		
		$(ins_old).animate(
			{width: "170px"},
			1000,
			function() {				
				$(ins_old).attr("class", "wrap hidden");
				$(ins_new).css("width", "170px");
				$(ins_new).attr("class", "wrap");
				$(ins_new).animate(
					{width: "650px"},
					1000,
					function(){
						
					}
				);
			}
		);
	}

	$(".content-page #container #main #curPage").html(ins_des);
}

function fadeOutFadeIn(fout, fin){
	$(fout).fadeOut(
		500,
		function () {
			$(fin).fadeIn(
				500,
				function(){
					if(fout == 'testimonials')
						$(".book_wrapper").css("display","none");
					if(fout == 'portfolio')
						$("#clientpage").css("display","none");
				}
			);
		}
	);
}

function changeDetail(container, id_new) {
	//$(".book_wrapper").css("display","none");
	//$("#clientpage").css("display","none");	
	
	
	var old_id = '#' + container + " #" + $("#" + container + " .desc .curDisplay").html();
	var new_id = '#' + container + " #" + id_new;
	fadeOutFadeIn(old_id, new_id);
	
	if(id_new == 'testimonials')
		fadeOutFadeIn("#clientpage", ".book_wrapper");
			
	if(id_new == 'portfolio')
		fadeOutFadeIn(".book_wrapper", "#clientpage");
	
	$("#" + container + " .desc .curDisplay").html(id_new);
}

function validate_required(field,alerttxt)
{
	with (field){
		if (value==null||value=="")
			{alert(alerttxt);return false;}
		else {return true}
    }

}

function validate_email(field,alerttxt)
{
	with (field){
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) 
			{alert(alerttxt);return false;}
		else {return true;}
    }

}

function validate_form(thisform)
{
    with (thisform){
		if (validate_required(name,"Please enter your name")==false)
			{name.focus();return false;}
		if (validate_required(telephone,"Please enter you telephone number")==false)
			{telephone.focus();return false;}
		if (validate_required(email,"Please enter you email")==false)
			{email.focus();return false;}
		if (validate_email(email,"Sorry, this is not a valid e-mail address")==false)
			{email.focus();return false;}
		if (validate_required(message,"Please enter a message")==false)
			{message.focus();return false;}
    }
}

function loadContent(page, des, func) {
	$.ajax({
		type: 'GET',
		url: page,
		async: false,
		dataType: 'html',
		success: function(data) {
			var newData = $(des).html() + data;
			$(des).html(newData);
			if (func != null) {func();}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			$(des).html(textStatus);
		}
	});
}
