	
	$(document).ready(function() {
	
                
		var winWidth = $(window).width();
		var winHeight = $(window).height();
               
                
		// measure window width and height if window was resized
		$(window).resize(function() {
			var winWidth = $(window).width();
			var winHeight = $(window).height();
			
			// change bg image
			var imgWidth = $("#bgImage").children('img').width();
			var imgHeight = $("#bgImage").children('img').height();
			var ratio = imgWidth/imgHeight;
			var imgTop = (winHeight/2 - imgHeight/2) / ratio;
			var ldngLeft = winWidth/2 - 110;
			var ldngTop = winHeight/2 - 10;
			
			$('#loading').css({left: ldngLeft, top: ldngTop});
			$("#bgImage").children('img').css({width: winWidth, top: imgTop});
                        
                        set_height_studies();
                        generate_popup();
		});
		
		
		$('#bgImage').css({
			  width: winWidth, height: winHeight	  
		});
		$(this).children('img').css({width: winWidth});
		
			
		// open menu items by default
		if(fAboutUs == 1 && fAboutUsContent == 1){
			ResetPos('#about-us');
			Move('#latest-news', 810);Move('#contact-us', 810);Move('#client-list', 810);  
			ShowHide('#about-us-content', 30, aboutContentWd);
		}
		
		if(fClientList == 1 && fClientListContent == 1){
			ResetPos('#client-list');
			Move('#latest-news', clientContentWd);Move('#contact-us', clientContentWd); 
			ShowHide('#client-list-content', 68, clientContentWd);
		}
		
		if(fContactUs == 1 && fContactUsContent == 1){
			ResetPos('#contact-us');
			Move('#latest-news', 360); 
			ShowHide('#contact-us-content', 106, contactContentWd);
		}
		
		if(fLatestNews == 1 && fLatestNewsContent == 1){
			ResetPos('#latest-news');
			ShowHide('#latest-news-content', 144, newsContentWd);
		}
		////////////////////////
		
		// ################################
		// open close menu items onclick
		// ################################
				
		$('#about-us').click(function(){
			if(fAboutUs == 0 && fClientList != 0 && fContactUs != 0 && fLatestNews != 0){
				Move('#latest-news', 810);Move('#contact-us', 810);Move('#client-list', 810);  
				ShowHide('#about-us-content', 30, aboutContentWd);
				CloseInfoBoxes('#latest-news-archive', '.postInfo', '.clientInfo');
			}else{
				ResetPos('#about-us');
				Move('#latest-news', 810);Move('#contact-us', 810);Move('#client-list', 810);  
				ShowHide('#about-us-content', 30, aboutContentWd);
				CloseInfoBoxes('#latest-news-archive', '.postInfo', '.clientInfo');
			}
			
			$('#scrollbar4').tinyscrollbar({sizethumb: 300});
		});
		
		$('#client-list').click(function(){
			if(fAboutUs == 0 && fClientList == 0 && fContactUs != 0 && fLatestNews != 0){
				Move('#latest-news', clientContentWd);Move('#contact-us', clientContentWd); 
				ShowHide('#client-list-content', 68, clientContentWd);
				CloseInfoBoxes('#latest-news-archive', '.postInfo', '.clientInfo');
			}else{
				ResetPos('#client-list');
				Move('#latest-news', clientContentWd);Move('#contact-us', clientContentWd); 
				ShowHide('#client-list-content', 68,clientContentWd);
				CloseInfoBoxes('#latest-news-archive', '.postInfo', '.clientInfo');
			}
			
		});
		
		$('#contact-us').click(function(){
			//$(location).attr('href', '/index.php/contacts');
			if(fAboutUs == 0 && fClientList == 0 && fContactUs == 0 && fLatestNews != 0){
				Move('#latest-news', 360);
				ShowHide('#contact-us-content', 106, contactContentWd);
				CloseInfoBoxes('#latest-news-archive', '.postInfo', '.clientInfo');
			}else{
				ResetPos('#contact-us');
				Move('#latest-news', 360);
				ShowHide('#contact-us-content', 106, contactContentWd);
				CloseInfoBoxes('#latest-news-archive', '.postInfo', '.clientInfo');
			}
		});
		
		$('#latest-news').click(function(){
			$(location).attr('href', 'http://blog.double-europe.com/');
			/*if(fAboutUs == 0 && fClientList == 0 && fContactUs == 0 && fLatestNews == 0){
				ShowHide('#latest-news-content', 144, newsContentWd);
				CloseInfoBoxes('#latest-news-archive', '.postInfo', '.clientInfo');
			}else{
				ResetPos('#latest-news');
				ShowHide('#latest-news-content', 144, newsContentWd);
				CloseInfoBoxes('#latest-news-archive', '.postInfo', '.clientInfo');
			}*/
		});
		// ##################################
		
		
		$('.arrowDown').click(function(){
			var clientListEl = $('#items > li > a').get(0);
			var h1 = $(clientListEl).height();
		});
		
		// info box show
		$('.infoBox').click(function() {
			
			// close post Info overlay box if it is open
			if($('.postInfo').css('display') == 'block') $('.postInfo').hide('fast');
            if (parseInt($('#studies-wrapper').css('right')) == 0){
                $('#studies-wrapper').animate({right: "-177px"}, 200);
            }

			$('#popup-studies').show();	
			
			//var cHeight = $(window).height()/2; // height of the clientInfo div
			//var cTop = $(window).height()/2 - cHeight/2; // distance from top of the clientInfo div
			
			if($('#latest-news-content').css('display') == 'block') var offset = 470;
			else if($('#contact-us-content').css('display') == 'block') var offset = 560;
			else if($('#about-us-content').css('display') == 'block') var offset = 980;
			else var offset = 430;
			
			
			//$('.clientInfo').css({width: $(window).width()-offset, height: cHeight, top: cTop})
			
			//$('.clientInfo').show('slow');
			//$('#scrollbar3').height(cHeight-100);
			 
			//$('.clientInfoPad').find(".viewport").height(cHeight-100);
			//alert(cHeight);
			//$('#scrollbar3').tinyscrollbar({sizethumb: 100});
			
			//$(this).hide('fast');	
			generate_popup();	
		});
		//////////////////////
		
		// info box hide
		$('.clientInfoHide').click(function() {
			$('.clientInfo').hide('slow');
			$('.infoBox').show('fast');		
		});
		
		/*$('.clientInfo').click(function() {
			$(this).hide('slow');
			$('.infoBox').show('fast');		
		});*/
		////////////////////////////////
		
		// hide post box
		$('.postInfoHide').click(function(){
			$('.postInfo').hide('slow');
		});
		
		
		// initialize scrollable with mousewheel support
		$(".clientListBox").scrollable({vertical: true, mousewheel: true});
		$(".blogPosts").scrollable({vertical: true, mousewheel: true});
		$(".images-gallery").scrollable({vertical: true, mousewheel: true});
                
		// change backgound image if client thumb is clicked
				
		
		$('.img_path').live('click', function(){
			var winWidth = $(window).width();
			var winHeight = $(window).height();
			
			var path = $(this).attr("href");
			
			var imgWidth = $("#bgImage").children('img').width();
			var imgHeight = $("#bgImage").children('img').height();
			var ratio = imgWidth/imgHeight;
			var imgTop = (winHeight/2 - imgHeight/2) / ratio;
			var ldngLeft = winWidth/2 - 110;
			var ldngTop = winHeight/2 - 10;
			
			$('#loading').css({left: ldngLeft, top: ldngTop});
			$('#loading').show(1);
			
			$("#bgImage").fadeOut("fast", function() { 
				$(this).children('img').attr("src", path); 
				$(this).delay(200).fadeIn("slow"); 
			}); 
			
			$('#loading').delay(200).hide(1);
			// position backround client image into the center of the screen
			$("#bgImage").children('img').css({width: winWidth, top: imgTop});
			
			return false;
		});
		
		// open/close news archive
		$('.archiveBtn').click(function(){
			
			closePostInfo();
			if($('#latest-news-archive').css('display') == 'none'){
				$('#latest-news-archive').show('slow');
				$('#scrollbar2 .viewport').height($('.latest-news-archivePad').height());
				$('#scrollbar2').tinyscrollbar();
			}else{
				$('#latest-news-archive').hide('slow');
			}
			
		});
                
                // hide\show pgroject Gallery block
                $('#studies-btn').bind('click', function(){
                    if (parseInt($('#studies-wrapper').css('right')) < 0){
                        $('#studies-wrapper').animate({right: "0px"}, 200);
                    }else{
                        $('#studies-wrapper').animate({right: "-177px"}, 200);
                    }
                })
                $('#studies-btn').click();
                
                $('.close-popup').bind('click', function(){
                    $('#popup-studies').hide();
                });
                
				$('#client_logo').bind('click', function(){
					$('#studies-btn').click();
				});
                /*
                $('#main-studies-content .items a').bind('click', function(){
                    $('#popup-studies').show();
                    generate_popup();
                    return false;
                });
                */

                set_height_studies();
                
		// send ajax to get client project images 		
		$('.item').click(function(){
			var projectName = $(this).find(".title-item").html();
			var projectId = $(this).find("span.id").html();
			
			
			//////////
			// changing background image
			var winWidth = $(window).width();
			var winHeight = $(window).height();
			var path = $(this).find("span.big").html();
						
			var imgWidth = $("#bgImage").children('img').width();
			var imgHeight = $("#bgImage").children('img').height();
			var ratio = imgWidth/imgHeight;
			var imgTop = (winHeight/2 - imgHeight/2) / ratio;
			var ldngLeft = winWidth/2 - 110;
			var ldngTop = winHeight/2 - 10;
			
			$('#loading').css({left: ldngLeft, top: ldngTop});
			$('#loading').show(1);
			
			$("#bgImage").fadeOut("fast", function() { 
				$(this).children('img').attr("src", path); 
				$(this).delay(200).fadeIn("slow"); 
			}); 
			
			$('#loading').delay(200).hide(1);
			// position backround client image into the center of the screen
			$("#bgImage").children('img').css({width: winWidth, top: imgTop});
			/////////
			
			// get project images
			$.ajax({
				url: '/index.php/ajax_handler/get_clientproject_images/',
				type: "POST",
				data: ({project : projectName, projectid : projectId}),
				dataType: "html",
				async:false,
				cache: false,
				
				beforeSend: function(data) {
				},
				success: function(data) {
					$('#main-studies-content').find(".items").html(data);
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					alert('ERROR: '+XMLHttpRequest+'\n'+textStatus +'\n' + errorThrown);
				},
				complete: function(data) {		
				}			
			});
			
			// get project text
			$.ajax({
				url: '/index.php/ajax_handler/get_clientproject_info/',
				type: "POST",
				data: ({project : projectName, projectid : projectId}),
				dataType: "html",
				async:false,
				cache: false,
				
				beforeSend: function(data) {
				},
				success: function(data) {
					$('#popup-studies').find(".title-popup").html(projectName);
					$('#popup-studies').find(".overview").html(data);
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					alert('ERROR: '+XMLHttpRequest+'\n'+textStatus +'\n' + errorThrown);
				},
				complete: function(data) {
                                    $('.infoBox').click();
				}			
			});
		});
				
	});
	
	
	
	
	// closes archive, post info and other popups that shouldn't be shown when menu bars are open
	function CloseInfoBoxes(){
		for(var i = 0; i<arguments.length; i++){
			if($(arguments[i]).css('display') != 'none'){
				if(arguments[i] == '.clientInfo' && $('.infoBox').css('display') == 'none'){
					$('.infoBox').show('fast');
				}
				$(arguments[i]).hide('fast');
			}
		}
	}
	
	
	// show/hide menu info bar
	function ShowHide(el, offset, wd)
	{
		var width = $(el).css("width");
		
		if (width == '0px'){
			$(el).css('display', 'block');
			$(el).animate({width: wd+"px"});
		}else{
			$(el).animate({width: "0px"});
			$(el).css('display', 'none');
		}
		
		if(el == '#about-us-content') {(fAboutUsContent == 0) ? fAboutUsContent = offset : fAboutUsContent = 0;}
		if(el == '#client-list-content') {(fClientListContent == 0) ? fClientListContent = offset : fClientListContent = 0;}
		if(el == '#contact-us-content') {(fContactUsContent == 0) ? fContactUsContent = offset : fContactUsContent = 0;}
		if(el == '#latest-news-content') {(fLatestNewsContent == 0) ? fLatestNewsContent = offset : fLatestNewsContent = 0;}
	}
	
	// open close menu items on click 
	// el - element we want to open/close
	// offset - distance in pixels we want to move
	function Move(el, offset)
	{
		if(el == '#about-us') {
			(fAboutUs == 0) ? $(el).animate({'marginLeft' : "+="+offset+"px"}) : $(el).animate({'marginLeft' : "-="+offset+"px"});
		}
		if(el == '#client-list') {
			(fClientList == 0) ? $(el).animate({'marginLeft' : "+="+offset+"px"}) : $(el).animate({'marginLeft' : "-="+offset+"px"});
		}
		if(el == '#contact-us') {
			(fContactUs == 0) ? $(el).animate({'marginLeft' : "+="+offset+"px"}) : $(el).animate({'marginLeft' : "-="+offset+"px"});
		}
		if(el == '#latest-news') {
			(fLatestNews == 0) ? $(el).animate({'marginLeft' : "+="+offset+"px"}) : $(el).animate({'marginLeft' : "-="+offset+"px"});
		}
		
		if(el == '#about-us') {(fAboutUs == 0) ? fAboutUs=offset : fAboutUs=0;}
		if(el == '#client-list') {(fClientList == 0) ? fClientList=offset : fClientList=0;}
		if(el == '#contact-us') {(fContactUs == 0) ? fContactUs=offset : fContactUs=0;}
		if(el == '#latest-news') {(fLatestNews == 0) ? fLatestNews=offset : fLatestNews=0;}
	}
	
	// resets the position of menu bar
	function ResetPos(el){
		
		if(fAboutUs != 0) {$('#about-us').css('margin-left', '');}
		if(fAboutUsContent != 0) {$('#about-us-content').css('width', '0px');$('#about-us-content').hide(1);}
		
		if(fClientList != 0) {$('#client-list').css('margin-left', '');}
		if(fClientListContent != 0) {$('#client-list-content').css('width', '0px');$('#client-list-content').hide(1);}
		
		if(fContactUs != 0) {$('#contact-us').css('margin-left', '');}
		if(fContactUsContent != 0) {$('#contact-us-content').css('width', '0px');$('#contact-us-content').hide(1);}			
		
		if(fLatestNews != 0) $('#latest-news').css('margin-left', '');
		if(fLatestNewsContent != 0) {$('#latest-news-content').css('width', '0px');$('#latest-news-content').hide(1);}
		
		fAboutUs = fClientList = fContactUs = fLatestNews = 0;
		fAboutUsContent = fClientListContent = fContactUsContent = fLatestNewsContent = 0;
	
	}
	
	// changes client logo image on mouse over
	$(function() {
	    $('.rollover').hover(function() {
	        var currentImg = $(this).attr('src');
	        $(this).attr('src', $(this).attr('hover'));
	        $(this).attr('hover', currentImg);
	    }, function() {
	        var currentImg = $(this).attr('src');
	        $(this).attr('src', $(this).attr('hover'));
	        $(this).attr('hover', currentImg);
	    });
	});	
	
	// images preloader
	function preloadImages(){
		for(var i = 0; i<arguments.length; i++){
		  jQuery("<img>").attr("src", arguments[i]);
		}
	};

	
	// Loads post content with ajax
	function loadPostContent(id){
		if (!id) {
			alert('Please select post to show!');
			return;
		}
		
		var pHeight = $(window).height()/2; // height of the postInfo div
		var pTop = $(window).height()/2 - pHeight/2; // distance from top of the postInfo div
		
		var ldngLeft = $(window).width()/2 + 135;
		var ldngTop = pTop + pHeight/2;
		
		$.ajax({
			cache: false,
			type: 'POST',
			async: false,
			dataType: 'json',
			url: '/index.php/ajax_handler/get_blog_item',
			data: 'id=' + id,
			beforeSend: function(data) {
				$('#loading').css({width: 220, left: ldngLeft, top: ldngTop});
				$('#loading').show();			
			},
			success: function(data) {
				if($('#latest-news-archive').css('display') == 'block') $('#latest-news-archive').hide();
				
				$(".postInfoPad").html(data.content);
				$('.postInfo').css({width: $(window).width()-470, height: pHeight, top: pTop})
				$('#scrollbar1').width($('.postInfo').width() - 390);
				$('#scrollbar1').height($('.postInfo').height() - 20);
				
				// close clientInfo overlay box before opening postInfo overlay box
				if($('.clientInfo').css('display') == 'block') $('.clientInfo').hide('fast');
				$('.infoBox').show('fast');
				
				$('.postInfo').show('slow');	
			},
			complete: function(data) {		
				$('#loading').hide();
				$('#scrollbar1').tinyscrollbar();	
			}			
		});		
	}
	
	function loadClientDefaultImageText(id){
		
		$.ajax({
				url: '/index.php/ajax_handler/get_text_main/',
				type: "POST",
				data: ({id : id}),
				dataType: "html",
				async:false,
				cache: false,
				
				beforeSend: function(data) {
					//$('.postLoading').show();
				},
				success: function(data) {
					$('.clientInfoPad').find(".overview").html(data);
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					alert('ERROR: '+XMLHttpRequest+'\n'+textStatus +'\n' + errorThrown);
				},
				complete: function(data) {	
					$('.infoBox').click();
					//$('.postLoading').hide();				
				}			
		});
	}
	
	
	// closes archive div
	function close_archive(){
		$('#latest-news-archive').hide('fast');
	}
	
	// closes post div
	function closePostInfo(){
		if($('.postInfo').css('display') == 'block') $('.postInfo').hide('fast');
	}
	
	// in case post with gallery open - change main image src by clicking on the thumbnails
	function loadGalleryMain(path){
		
			$("#galleryMain").fadeOut("fast", function() { 
			  $(this).attr("src", path); 
			  //$(this).delay(200).fadeIn("slow"); 
			  $(this).fadeIn("slow"); 
			}); 
	}	
        
        // set height for studies block
        function set_height_studies(){
            $('#studies-wrapper').height(($(window).height() - 320));
            $('.images-gallery').height(($(window).height() - 320));
        }
        
        // create popup for studies block
        
        function generate_popup(){
            $('#popup-studies').css('width', $(window).width() - 690);  
            $('.content-popup').tinyscrollbar();
        }
