//Declare global variables here
var menuOver = false;

$(document).ready(function() {
	//Attach Facebox to REL links
	$.facebox.settings.opacity = 0.5;
	$("a[rel*=facebox]").facebox();
   
	//Preload images
	$.preloadImages("../img/success-tick.png","../img/warning-icon.png","../img/news-arrow-down.png");
	
	//Drop down menu
	$("#navigation .services").mouseover(function(){
    	dropDown("services","429","100");
    });
    $("#navigation .projects").mouseover(function(){
    	dropDown("projects","272","150");
    });
    
    $("#navigation .services").mouseout(function(){
    	dropOut("services");
    });
    $("#navigation .projects").mouseout(function(){
    	dropOut("projects");
    });
    
    $(".drop-menu").mouseover(function(){
    	menuOver = true;
    });
    $(".drop-menu").mouseout(function(){
    	menuOver = false;
    });
    $("html").mouseover(function(){
    	if (menuOver == false) {
    		$(".drop-menu").css({
				"left": "-10000em"
			});
			$("#navigation li a").css({
				"background-position": "top left"
			});
			$("#navigation li a:hover").css({
				"background-position": "top right"
			});
			stabaliseSelected();
    	}
    });
   
	//News tree
	$(".level2").hide();
	$(".level3").hide();
	$(".level3 li a").hover(
		function () {
			$(this).css("background", "#b8dffd");
		}, 
		function () {
			$(this).css("background", "none");
		}
	);
	//Open up news tree onLoad
	var initNewsCat = $("#initNewsCat").attr("value");
	var initNewsYear = $("#initNewsYear").attr("value");
	var initNewsMonth = $("#initNewsMonth").attr("value");
	var hook1 = ""+initNewsCat+"-"+initNewsYear+"";
	var hook2 = ""+initNewsCat+"-"+initNewsYear+""+initNewsMonth+"";
	newsTree(""+hook1+"","1");
	newsTree(""+hook2+"","2");
	
	//Polaroid
	$(".polaroid").click(function() {
		$(".polaroid").removeClass("polaroidSelected")
		$(".polaroid").addClass("polaroid");
		$(this).addClass("polaroidSelected");
		var polaroidID = $(this).attr("rel");
			$.get( "../actions/team-get.php", {
				id: polaroidID },
				function(response) {
					response = $.trim(response);
					$("#content1-a").fadeOut(500);
					setTimeout(function () {
						$("#content1-a").html(response);
						sIFR.replace(helvetica, { selector: '#content1-a h2', wmode: 'transparent', css: ['.sIFR-root { font-weight: bold; color: #ffffff; }'], ratios: [8, 1.41, 10, 1.32, 11, 1.31, 15, 1.29, 19, 1.28, 26, 1.25, 31, 1.24, 37, 1.23, 41, 1.22, 42, 1.23, 53, 1.22, 56, 1.21, 57, 1.22, 92, 1.21, 93, 1.2, 96, 1.21, 98, 1.2, 100, 1.21, 103, 1.2, 104, 1.21, 108, 1.2, 109, 1.21, 113, 1.2, 114, 1.21, 1.2] });
					}, 500);
					$("#content1-a").fadeIn(500);
			});
		return false;
	});
	
	//Projects
	$(".project-select").click(function() {
		$(".project-select").css( { "background": "#3785bd" } );
		$(this).css( { "background": "#eec045" } );
		var projectID = $(this).attr("rel");
			$.get( "../actions/project-get.php", {
				id: projectID },
				function(response) {
					response = $.trim(response);
					$("#project").fadeOut(500);
					setTimeout(function () {
						$("#project").html(response);
						sIFR.replace(helvetica, { selector: '#project h1', wmode: 'transparent', css: ['.sIFR-root { font-weight: bold; color: #ffffff; }'], ratios: [8, 1.41, 10, 1.32, 11, 1.31, 15, 1.29, 19, 1.28, 26, 1.25, 31, 1.24, 37, 1.23, 41, 1.22, 42, 1.23, 53, 1.22, 56, 1.21, 57, 1.22, 92, 1.21, 93, 1.2, 96, 1.21, 98, 1.2, 100, 1.21, 103, 1.2, 104, 1.21, 108, 1.2, 109, 1.21, 113, 1.2, 114, 1.21, 1.2] });
					}, 500);
					$("#project").fadeIn(500);
			});
		return false;
	});
	
	//News change article
	//$(".news-link").click(function() {
	//	var newsID = $(this).attr("rel");
	//		$.get( "../actions/news-get.php", {
	//			id: newsID },
	//			function(response) {
	//				response = $.trim(response);
	//				$("#content1-b").fadeOut(500);
	//				setTimeout(function () {
	//					$("#content1-b").html(response);
	//					sIFR.replace(helveticaBlack, { selector: '#content1-b h2', wmode: 'transparent', css: ['.sIFR-root { font-weight: bold; color: #202020; }'], ratios: [8, 1.41, 10, 1.32, 11, 1.31, 15, 1.29, 19, 1.28, 26, 1.25, 31, 1.24, 37, 1.23, 41, 1.22, 42, 1.23, 53, 1.22, 56, 1.21, 57, 1.22, 92, 1.21, 93, 1.2, 96, 1.21, 98, 1.2, 100, 1.21, 103, 1.2, 104, 1.21, 108, 1.2, 109, 1.21, 113, 1.2, 114, 1.21, 1.2] });
	//					var script = 'http://s7.addthis.com/js/250/addthis_widget.js#domready=1';
	//					if (window.addthis){
    //						window.addthis = null;
	//					}
	//					$.getScript( script );
	//				}, 500);
	//				$("#content1-b").fadeIn(500);
	//		});
	//	return false;
	//});
	
   //Tweet jQuery API
	$("#tweet-content").tweet({
		username: "renmedia",
		join_text: "auto",
		avatar_size: 0,
		count: 2,
		auto_join_text_default: "we said,",
		auto_join_text_ed: "we",
		auto_join_text_ing: "we were",
		auto_join_text_reply: "we replied to",
		auto_join_text_url: "we were checking out",
		loading_text: "loading tweets..."
	});
	
	//China news ticker
	$("#china-ticker").innerfade({
		animationtype: "fade",
		speed: 750,
		timeout: 5000,
		type: "sequence",
		containerheight: "1em"
	});
	
	//Ren latest (homepage) ticker
	$("#ren-ticker").vTicker({
		speed: 1000,
		pause: 5000,
		showItems: 4,
		animation: "fade",
		mousePause: true
	});
	
	//Ren latest ticker
	$("#latest-ren-ticker").vTicker({
		speed: 1000,
		pause: 5000,
		showItems: 2,
		animation: "fade",
		mousePause: true
	});
	
	//Make sure that SIFR works when a Facebox is loaded
	$(document).bind("reveal.facebox", function() {
		sIFR.replace(helvetica, { selector: '.helvetica', wmode: 'transparent', css: ['.sIFR-root { font-weight: bold; color: #ffffff; }'], ratios: [8, 1.41, 10, 1.32, 11, 1.31, 15, 1.29, 19, 1.28, 26, 1.25, 31, 1.24, 37, 1.23, 41, 1.22, 42, 1.23, 53, 1.22, 56, 1.21, 57, 1.22, 92, 1.21, 93, 1.2, 96, 1.21, 98, 1.2, 100, 1.21, 103, 1.2, 104, 1.21, 108, 1.2, 109, 1.21, 113, 1.2, 114, 1.21, 1.2] });
	});
});

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}
function dropDown(hook,position,width) {
	menuOver = true;
	$(".drop-menu").css({
		"left": "-10000em"
	});
	$("#menu-"+hook+"").css({
		"left": ""+position+"px"
	});
	$("#navigation li a").css({
		"background-position": "top left"
	});
	stabaliseSelected();
	$("#navigation ."+hook+" a").css({
		"background-position": "top right"
	});
}
function dropOut(hook) {
	menuOver = false;
}
function stabaliseSelected() {
	$("#selectHome #navigation .home a").css({
		"background-position": "top right"
	});
	$("#selectAbout #navigation .about a").css({
		"background-position": "top right"
	});
	$("#selectProjects #navigation .projects a").css({
		"background-position": "top right"
	});
	$("#selectServices #navigation .services a").css({
		"background-position": "top right"
	});
	$("#selectNews #navigation .news a").css({
		"background-position": "top right"
	});
	$("#selectClients #navigation .clients a").css({
		"background-position": "top right"
	});
	$("#selectContact #navigation .contact a").css({
		"background-position": "top right"
	});
}
function newsTree(hook, level) {
	if (level == 1) {
		$(".level2").slideUp();
		$(".level3").slideUp();
		$(".level1 li a").css({
			"background": "url(../img/news-arrow-right.png) no-repeat center left"
		});
		$(".level3 li a").css({
			"background": "none", 
		});
	}
	if (level == 2) {
		$(".level3").slideUp();
		$(".level2 li a").css({
			"background": "url(../img/news-arrow-right.png) no-repeat center left"
		});
		$(".level3 li a").css({
			"background": "none", 
		});

	}
	$("#link"+hook+" a").css({
		"background": "url(../img/news-arrow-down.png) no-repeat center left"
	});
	$("#menu"+hook+"").slideDown();
}
