$(document).ready(function(){
	//=======================================================================================================
	// cookie management functions
	//=======================================================================================================
	function createCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}
	
	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	
	function eraseCookie(name) {
		createCookie(name,"",-1);
	}

	//=======================================================================================================
	// text settings
	//=======================================================================================================
	
	//hight contrast
	var hc = parseInt(readCookie("cookieHC"));
	if(hc == 1) $("*").not("a").css({fontWeight: 700, color: '#000'});
	
	$(".hc").click(function(){
		if(!hc) $("*").not("a").css({fontWeight: 700, color: '#000'});
		else $("*").not("a").css({fontWeight: "", color: ""});
		if(hc == 0) hc = 1; else hc = 0;
		createCookie("cookieHC", hc, 60);
		return false;
	});
	
	
	//getting font size from a cookie and setting it
	var fontSize = parseInt(readCookie("cookieFontSize"));
	if(!fontSize) fontSize = 0;
	
	$("body").css("font-size", "");
	$("body").css("font-size", parseInt(parseInt($("body").css("font-size")) + fontSize));
	
	//font size increase/decrease buttons
	$(".font-inc").click(function(){
		if(fontSize < 3) {
			fontSize++;
			createCookie("cookieFontSize", fontSize, 60);
			$("body").css("font-size", "");
			$("body").css("font-size", parseInt(parseInt($("body").css("font-size")) + fontSize));
		}
		return false;
	});
	$(".font-dec").click(function(){
		if(fontSize > -3) {
			fontSize--;
			createCookie("cookieFontSize", fontSize, 60);
			$("body").css("font-size", "");
			$("body").css("font-size", parseInt(parseInt($("body").css("font-size")) + fontSize));
		}
		return false;
	});
	
	//=======================================================================================================
	// projects slider
	//=======================================================================================================
	
	//slider init
	var itemWidth = 130;
	var slideCount = 6;
	var currentPos = 0;
	var listWidth = $(".slider li").length * itemWidth;
	var jumpLength = slideCount * itemWidth;
	var rest = ($(".slider li").length % slideCount) * itemWidth;
	if(rest == 0) rest = jumpLength;
	var minX = jumpLength - listWidth;
	if(listWidth <= jumpLength) minX = 0;
	else if(minX > 0 - rest) minX = 0 - rest;
	$(".slider ul").css("width", listWidth + 31 + "px");
	
	//slider item element states	
	var currentItem = 0;
	var animSpeed = 300;
	var liSelected = {top: -6, left: 0, width: 161, height: 86, marginLeft: -3, marginRight: -15};
	var liNormal = {top: 0, width: 137, height: 75, marginLeft: 0, marginRight: -7};
	var liSelectedA = {width: 161, height: 86, lineHeight: 86};
	var liNormalA = {width: 137, height: 75, lineHeight: 75};
	var liSelectedAImg = {marginTop: 8, width: 145, height: 71};
	var liNormalAImg = {marginTop: 7, marginLeft: 6, width: 125, height: 61};
	var liSelectedImgBack = {width: 161, height: 86};
	var liNormalImgBack = {width: 137, height: 75};
	
	//preloading slide background
	var bkgImg = new Image;
	if($.browser.msie && $.browser.version < 7) bkgImg.src = 'http://www.bravomedia.co.uk/bravo_n/images/ie6/selected.gif';
	else bkgImg.src = 'http://www.bravomedia.co.uk/bravo_n/images/selected.png';

	//moving the slider to and fro
	//--------------------------------------------------------------------
	$(".slide-forward").click(function(){
		if(currentPos > minX + jumpLength) currentPos = currentPos - jumpLength;
		else if(currentPos >= minX + rest) currentPos = currentPos - rest;
		else currentPos = minX;
		$(".slider ul").animate({left: currentPos}, 1000, "swing");
		return false;
	});
	$(".slide-back").click(function(){
		if(currentPos < 0 - jumpLength) currentPos = currentPos + jumpLength;
		else if(currentPos >= 0 - rest && currentPos < 0) currentPos = currentPos + rest;
		else currentPos = 0;
		$(".slider ul").animate({left: currentPos}, 1000, "swing");
		return false;
	});
		
	//hiding unwanted http://www.bravomedia.co.uk/bravo_n/images//--------------------------------------------------------------------
	$(".presentation img").not($($(".presentation img").get(0))).hide();
	$(".project-descr li").not($($(".project-descr li").get(0))).hide();
	
	
	//this happens when a slider link is clicked:
	//--------------------------------------------------------------------
	$(".slider a").click(function(){
		var oldItem = currentItem;
		currentItem = $(this).attr("rel");
		if($(this).attr("rel") == oldItem) return false;
		$($(".presentation img").get(currentItem)).show();
		$($(".project-descr li").get(currentItem)).fadeIn();
		$($(".presentation img").get(oldItem)).hide();
		$($(".project-descr li").get(oldItem)).fadeOut();
		return false;
	});
	
	$(".slider a img").mouseover(function(){
		$(this).parent().parent().addClass("selected");
		if($.browser.msie && $.browser.version < 7) $(this).parent().parent().prepend('<img src="http://www.bravomedia.co.uk/bravo_n/images/ie6/selected.gif" alt="" class="back" />');
		else $(this).parent().parent().prepend('<img src="http://www.bravomedia.co.uk/bravo_n/images/selected.png" alt="" class="back" />');
		$(this).parent().parent().animate(liSelected, animSpeed);
		$(this).parent().animate(liSelectedA, animSpeed);
		$(this).animate(liSelectedAImg, animSpeed);
		$(this).parent().siblings("img.back").animate(liSelectedImgBack, animSpeed);
	});
	$(".slider a img").mouseout(function(){
		$(this).parent().parent().animate(liNormal, animSpeed);
		$(this).parent().animate(liNormalA, animSpeed);
		$(this).animate(liNormalAImg, animSpeed);
		$(this).parent().siblings("img.back").animate(liNormalImgBack, animSpeed, function(){$(this).parent().removeClass("selected"); $(this).remove();});
	});

	//=======================================================================================================
	// modal window with projects inside
	//=======================================================================================================
	
	// function that changes pictures in the modal window and its size & position
	//--------------------------------------------------------------------
	function changePic(oldPic, newPic, w, h){
		var resizeSpeed = 1000;
		$(".loader").show();
		oldPic.hide();
		$(".prev, .next").hide();
		var margin = 0 - (w + 50)/2;
		if($.browser.msie) {
			$(".modal").animate({marginLeft: margin, width: w+50, height: h + 113}, resizeSpeed);
			$(".modal-top").animate({width: w, height: h + 63}, resizeSpeed);
			$(".modal-bottom").animate({width: w, height: h}, resizeSpeed);
		}
		else $(".modal").animate({marginLeft: margin}, resizeSpeed);
		$(".modal-w").animate({height: h}, resizeSpeed);
		$(".modal-e").animate({height: h}, resizeSpeed);
		$(".modal-img-wrapper").animate({width: w, height: h}, resizeSpeed, function(){$(".loader").hide(); newPic.fadeIn(); if($.browser.msie) $(".prev, .next").show(); else $(".prev, .next").fadeIn();});
		
		$(".modal-bottom").children("ul").children("li").children("a").removeClass("current");
		$($(".modal-bottom").children("ul").children("li").children("a").get(currentPic)).addClass("current");
	}
	
	//function opening the modal window
	//--------------------------------------------------------------------
	var currentPic = 0;
	function showModal(index){
		$(".modal-img-wrapper").children("img").remove();
		
		//putting content from html to the modal window		
		$(".modal-img-wrapper").prepend($($(".project-images").get(index)).html());
		$(".modal img").not(".default").hide();
		$(".modal-bottom p").html($($(".project-description").get(index)).html());
		var imgCount = $(".modal-img-wrapper img").length;
		if(imgCount > 1){
			for(i = 0; i < imgCount; i++){
				var num = i + 1;
				if(i == 0) $(".modal-bottom").children("ul").append('<li><a href="#" rel="' + i + '" class="current">' + num + '</a></li>');
				else $(".modal-bottom").children("ul").append('<li><a href="#" rel="' + i + '">' + num + '</a></li>');
			}
		}
		
		//showing the modal window
		if($.browser.msie) {
			$(".overlay").css("height", $("body").height());
			$(".overlay, .modal").show();
		} else $(".overlay, .modal").fadeIn();
		
		//getting the default picture and setting modal window size and position
		currentPic = 0;
		var newPic = $($(".modal-img-wrapper").children("img").get(currentPic));
		var w = $($(".modal-img-wrapper").children("img").get(currentPic)).width() + 120;
		var h = $($(".modal-img-wrapper").children("img").get(currentPic)).height();
		changePic(newPic, newPic, w, h);
		
		//what happens when a screenshot number is clicked
		$(".img-switch a").click(function(){
			$(".img-switch a").removeClass("current");
			$(this).addClass("current");
			old = currentPic;
			currentPic = $(this).attr("rel");
			var oldPic = $($(".modal-img-wrapper").children("img").get(old));
			var newPic = $($(".modal-img-wrapper").children("img").get(currentPic));
			var w = $($(".modal-img-wrapper").children("img").get(currentPic)).width() + 120;
			var h = $($(".modal-img-wrapper").children("img").get(currentPic)).height();
			
			changePic(oldPic, newPic, w, h);
			return false;
		});
	}
	
	//closing the modal window
	//--------------------------------------------------------------------
	$(".modal a.close").click(function(){
		//clearing stuff
		currentPic = 0;
		$(".modal-img-wrapper").children("img").remove();
		$(".modal-bottom").children("p").html("");
		$(".modal-bottom").children("ul").html("");
		
		//hiding the modal
		if($.browser.msie) {
			$(".overlay").hide();
			$(this).parent().hide();
		} else {
			$(".overlay").fadeOut();
			$(this).parent().fadeOut();
		} return false;
	});
	
	// the "zoom" button opens the modal window
	//--------------------------------------------------------------------
	$("a.zoom, .presentation img").click(function() { showModal(currentItem); return false; });
	
	
	
	// show previous screenshot
	//--------------------------------------------------------------------
	$(".modal a.prev").click(function(){
		var old = currentPic;
		if(currentPic == 0) currentPic = $(".modal-img-wrapper").children("img").length - 1;
		else currentPic--;
		
		var oldPic = $($(".modal-img-wrapper").children("img").get(old));
		var newPic = $($(".modal-img-wrapper").children("img").get(currentPic));
		var w = $($(".modal-img-wrapper").children("img").get(currentPic)).width() + 120;
		var h = $($(".modal-img-wrapper").children("img").get(currentPic)).height();
		
		changePic(oldPic, newPic, w, h);
		return false;
	});
	
	// show next screenshot
	//--------------------------------------------------------------------
	$(".modal a.next").click(function(){
		var old = currentPic;
		if(currentPic == $(".modal-img-wrapper").children("img").length - 1) currentPic = 0;
		else currentPic++;
		
		var oldPic = $($(".modal-img-wrapper").children("img").get(old));
		var newPic = $($(".modal-img-wrapper").children("img").get(currentPic));
		var w = $($(".modal-img-wrapper").children("img").get(currentPic)).width() + 120;
		var h = $($(".modal-img-wrapper").children("img").get(currentPic)).height();
		
		changePic(oldPic, newPic, w, h);
		return false;
	});
	
	// show particular screenshot - accordingly to the number clicked
	//--------------------------------------------------------------------
	$(".img-switch a").click(function(){
		$(".img-switch a").removeClass("current");
		$(this).addClass("current");
		old = currentPic;
		currentPic = $(this).attr("rel");
		var oldPic = $($(".modal-img-wrapper").children("img").get(old));
		var newPic = $($(".modal-img-wrapper").children("img").get(currentPic));
		var w = $($(".modal-img-wrapper").children("img").get(currentPic)).width() + 120;
		var h = $($(".modal-img-wrapper").children("img").get(currentPic)).height();
		
		changePic(oldPic, newPic, w, h);
		return false;
	});
	

});
