
	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);
	}



	animate_speed = 1000;
	function topmenu_slide_next(){
		
		var topmenu_panel_width  = $("#topmenu_panel").width();
		var topmenu_current_left = parseInt($("#topmenu_panel_pos").css('left'));
		
		new_left = (topmenu_current_left - topmenu_panel_width);
		
		// animate
		$("#topmenu_panel_pos").animate({
			
			left: new_left + 'px'
			
		}, animate_speed);
	}
	
	
	function topmenu_slide_prev(){
		
		var topmenu_panel_width = $("#topmenu_panel").width();
		var topmenu_current_left = parseInt($("#topmenu_panel_pos").css('left'));
		
		// animate
		$("#topmenu_panel_pos").animate({
		
			left: (topmenu_current_left + topmenu_panel_width) + 'px'
			
		}, animate_speed);
		
	}
	
	
	function img_switch_list_link_click( el ){
		
		//if( $(this).attr('class') == "" ){
		if( !el.hasClass('active') ){
	
			// white BG ?
			$("BODY").removeClass('white_bg');
			if( el.hasClass('white_bg') ){ $("BODY").addClass('white_bg'); }
	
			// hide ballon
			hide_ballon();
			
			// remove active from all
			$("UL#img_switch_list LI A").removeClass("active");
			
			// show preloader
			ie_cache = "";
			if( !el.hasClass('cached') ){ show_preloader(); }
			if( $.browser.browser() == "Internet Explorer" ){ 
				show_preloader();
				ie_cache = "?random=" + Math.random()*99999; 
			}
			
			// load next img
			$('<img />')
			    .attr('src', el.attr('href') + ie_cache)
				.attr('width', '1000')
				.attr('id', 'work_img')
			    .load(function(){
			    	
					// remove current img
					$("#site_panel IMG#work_img").remove();
			   						   		
			   		// add img to site panel
					$("#site_panel").append($(this));
					
					// set img width to 100%
					$("#site_panel IMG").width('100%');
					
					// hide preloader
					hide_preloader();
					
					// show ballon ?
					show_ballon_for_active_link();	
					   			       
			    });
			    
					
			// add cached class
			el.addClass("cached");
			
			// mark this link active
			el.addClass("active");		

			
		}	
		
	}

	function calc_panel_height(){
		
		// get dims
		var viewportWidth = $(window).width();
		var viewportHeight = $(window).height();	
		
		return (viewportHeight - $("#top").height());
		
	}
	
	
	function hide_ballon(){
		
		// $("#baloon").css('left', '-9999px');
		
		//remove_timeout = setTimeout('$("#baloon").remove();', 100);
		
		$("#baloon").remove();
		
	}
	
	
	function hide_over_img(){
		
		$("IMG#over_img").remove();
		
	}
	
	
	function over_img_click_event(){
		
		$("IMG#over_img").click(function(){
			
			// remove img
			$(this).remove();
			
			// show ballon
			show_ballon( $("UL#img_switch_list LI A.active").attr('rel') );
			
			return false;
			
		})
		
	}
	
	
	function show_ballon_for_active_link(){
		
		ballon_filename = $("UL#img_switch_list LI A.active").attr('rel');
		
		if( ballon_filename != "" ){
			
			// hide prev ballon
			hide_ballon();
			
			// show ballon
			show_ballon( ballon_filename );
			
		}
		
	}
	
	
	function hide_small_overlay(){
		
		$("#small_overlay").remove();
		
	}
	
	function show_small_overlay(){
		
		$("#small_overlay").remove();
		
		$('#site_panel').append('<div id="small_overlay"></div>');
		//$("#small_overlay").css('opacity', '0.35');
		$("#small_overlay").css('opacity', '0.7');
		
	}
	
	
	function attach_ballon_click_event(){
		
		//$("#baloon").click(function(){
		$("#baloon").bind( 'mouseenter' , function(){
		
			img_dims 	= $("UL#img_switch_list LI A.active").attr('title');
			img_dims1 	= img_dims.split("-");

			// show small overlay
			show_small_overlay();

			// append BODY with over img			
			$('BODY').append('<img src="'+$(this).attr('href')+'" width="'+img_dims1[0]+'" height="'+img_dims1[1]+'" alt="" title="" id="over_img">');
		
			// attach img click event
			over_img_click_event();
			
			// remove ballon link
			// $(this).remove();
		
			return false;
			
		})
		.bind('mouseleave',function(){
			
			// hide small overlay
			hide_small_overlay();
			
			// remove over PNG
			$("IMG#over_img").remove();
			
			return false;
			
		})
		.click(function(){
			
			return false;
			
		});
		
	}
	
	
	
	function show_ballon( ballon_filename ){
		
		hide_over_img();
		
		if( ballon_filename != "" ){
			
			// append BODY with ballon link
			$('BODY').append('<a href="' + ballon_filename + '" id="baloon"></a>');

			
			// attach click event to ballon
			attach_ballon_click_event();
			
		}
		
	}
	
	
	
	function pre_page_adjust_height(){
		
		// get dims
		var viewportWidth = $(window).width();
		var viewportHeight = $(window).height();	
		
		$(".pre_page").eq(0).height( (viewportHeight - $("#top").height()) );
		
	}
	
	
	function show_preloader(){
		
		hide_ballon();
		
		if( $("#overlay").length == 0 ){
			
			// append BODY
			$('BODY').append('<div id="overlay"></div>');
			
			// set opacity
			$("#overlay").css('opacity', '0.7');

			
		}
		
	}
	
	
	function hide_preloader(){
		
		$("#overlay").remove();
		
	}
	
	
	function get_current_img_aspect_ratio(){
		
		// get active index
		var active_link_index = $("UL#img_switch_list LI A").index($("UL#img_switch_list LI A.active"));
		
		// get LI by index
		var img_ratio = $("UL#img_ratio LI").eq(active_link_index).text();
		
		return img_ratio;
		
	}
	
	
	
	function autoheight_arrows(){
		
		if( ($("#project_arrow_left").length && $("#project_arrow_right").length) ){
			
			panel_height = $("#site_panel").height();
			$("#project_arrow_left , #project_arrow_right").height(panel_height);
			
		}
		
	}
	
	

	function adjust_panel(){
				
		// on init
		panel_height = calc_panel_height();  
			
		$("#site_panel").height(panel_height);	
		
		// img height in px
		if( $.browser.browser() == "Internet Explorer" ){
			img_aspect_ratio = get_current_img_aspect_ratio();
			$("#site_panel IMG#work_img").height( parseInt($("#site_panel IMG#work_img").width() / img_aspect_ratio) );			
		}
		
		// $("#topmenu_panel_pos UL LI:first").text('');
		//$("#topmenu_panel_pos UL LI:first").text($(window).width() + " - " + $(window).height());
		
		// manually resize window
		//if( $("#site_panel").height() > $("#site_panel IMG#work_img").height() ){
	
		min_width = 990;
	
		if( ($(window).width() < min_width )
			|| ($("#site_panel").height() > $("#site_panel IMG#work_img").height()) ){			
		
			// calculate new IMG dimensions
			koef = parseInt($("#site_panel IMG#work_img").width() / $("#site_panel IMG#work_img").height());
			new_height = ($(window).height() + $("#top").height() ) * koef;
			//new_width  = new_height * koef;
			
			if( $(window).width() < min_width ) new_width 	 = min_width;
			else new_width 	 = $(window).width();
			//new_width 	 = 800;
			new_height 	 = $(window).height() - ($("#site_panel").height() - $("#site_panel IMG#work_img").height());
			new_height = new_height + 150;

			if( $.browser.browser() == "Internet Explorer" ){ 
				/*try { c_timeout = setTimeout("window.resizeTo(new_width, new_height)", 2000); }
				catch (err){ return false; }*/
			}
			else if( $.browser.browser() == "Safari" ){ new_height = new_height - 150; c_timeout = setTimeout("window.resizeTo(new_width, new_height)", 10); }
			else window.resizeTo(new_width, new_height);
			
		}

	}