$(window).load(function() {    
        var theWindow        = $(window),
            $bg              = $("#bg"),            
            aspectRatio      = $bg.width() / $bg.height();

        function resizeBg() {

                if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
                    $bg.parent()
                        .removeClass()
                        .addClass('bgheight');
                } else {
                    $bg.parent()
                        .removeClass()
                        .addClass('bgwidth');
                }

        }

        theWindow.resize(function() {
                resizeBg();
        }).trigger("resize");

});




var f=-10; // IE rotate

$(document).ready(function() {
// remove home page prompts
var removePromptsAfter=5000;
// $('#view-case, #view-case-bg').delay(removePromptsAfter).fadeOut(500);

	// ieRotate function
	function ieRotate(f){	
		$('#menu').rotate(f);
	}
	
	// check cookie for intro/menu state

	var introState=$.cookie('introState')	
	
	if (introState!="" && introState!=null){		
//	alert(introState)
		if (introState=='1'){
		$('#menu').addClass('up')
			if ($(".csstransforms").length < 1 ){ //ie
			$('#menu').rotate(-90);
			$('#menu').css({'top':'0'})
			$('#menuInner').css({'top':'0'})
			$('#menuInner').css({'left':'170'})
			}		
		}
		if (introState=='2'){
		$('#menu').addClass('open up')		
			$('.toggle-menu').addClass('open')
			if ($(".csstransforms").length < 1 ){ //ie
			$('#menu').rotate(-90);
			$('#menu').css({'top':'0'})
			$('#menuInner').css({'left':'0'})
			}		
		}
		if (introState=='3'){
			$('#menu').addClass('open').removeClass('up')
			$('.toggle-menu').addClass('open')
		}	
		if (introState=='4'){
		$('#menu').removeClass('open').removeClass('up')
		}

	}else{
		introState='3' //default position	
		$.cookie('introState', '3')
			$('#menu').addClass('open').removeClass('up')
			$('.toggle-menu').addClass('open')

	}




	// init tooltips
	$(function(){
			if ( !$.browser.msie ) {
			$("#menu a[title]").tipTip({defaultPosition:'right' });
			}
			$(".content-panel a[title]").tipTip({defaultPosition:'right' });
			$("#thumb-nav a[title]").tipTip({edgeOffset:10, defaultPosition:'top' });
	});



	$('.rotate').click(
		function(){
	
		var toggle = $('.toggle-menu');
				var $this = $('#menu');
				if ($this.hasClass('up')){				
					$.cookie('introState','3') // set to down open				
					var r=0
					$('#menu').css('border-spacing', '-90' );

		
					if ($(".csstransforms").length > 0 ){ /* Check for transforms */		
//						alert('csstransforms')
		
						$('#menu').animate({borderSpacing: 90 }, {						
						    step: function(now,fx) {
						    now=now-90
						      $(this).css('-webkit-transform','rotate('+now+'deg)');
						      $(this).css('-moz-transform','rotate('+now+'deg)'); 
						      $(this).css('transform','rotate('+now+'deg)');  					 
						    },
						    duration:500
						},'linear');
		
					}else{
	
					t=2
					//window.setTimeout(function() {ieRotate(-90);}, t*150)
					window.setTimeout(function() {ieRotate(-88);}, t*2)
					window.setTimeout(function() {ieRotate(-86);}, t*10)
					window.setTimeout(function() {ieRotate(-84);}, t*15)
					window.setTimeout(function() {ieRotate(-82);}, t*20)
					window.setTimeout(function() {ieRotate(-80);}, t*30)
					window.setTimeout(function() {ieRotate(-70);}, t*40)
					window.setTimeout(function() {ieRotate(-60);}, t*50)
					window.setTimeout(function() {ieRotate(-50);}, t*60)
					window.setTimeout(function() {ieRotate(-40);}, t*70)
					window.setTimeout(function() {ieRotate(-30);}, t*80)
					window.setTimeout(function() {ieRotate(-20);}, t*90)
					window.setTimeout(function() {ieRotate(-15);}, t*100)
					window.setTimeout(function() {ieRotate(-10);}, t*120)
					window.setTimeout(function() {ieRotate(-5);}, t*140)		
					window.setTimeout(function() {ieRotate(0);}, t*170)
					}

					$this.removeClass('up')
	
					var rotate = $('.rotate');
					rotate.animate({left:10}, 300, 'swing')
		
					var toggleMenu = $('.toggle-menu');			
					toggleMenu.animate({right:15}, 300, 'swing')
				
			}else{
			// menu is down
				var r=-90		
				$this.addClass('up')	
				toggle.addClass('open')

				$.cookie('introState','2') // set to up open				
				
				if ($(".csstransforms").length > 0 ){ /* Check for transforms */							
				
					$('#menu').css('border-spacing', '0' );
					var top=0
					   $('#menu').animate({borderSpacing: -90 }, {
					    step: function(now,fx) {
					      $(this).css('-webkit-transform','rotate('+now+'deg)');
					      $(this).css('-moz-transform','rotate('+now+'deg)'); 
					      $(this).css('transform','rotate('+now+'deg)');  
					      // $('#screen').text(Math.round(now) )					      			      
						top=top-1
					 
					    },
					    duration:'slow'
					},'linear');
					/*   .animate({top:-165},{ duration:300},'swing').animate({left:160},{ duration:200},'swing')  */ 

				}else{

					
//				for (f=0; f>-80; f=f-10){

				interval=200;
//				setTimeout( "ieRotate", 1000  );
//				ieRotate()
//				$('#menu').rotate(f);

//				$('#menu').css('opacity', t/100)
//				$('#screen').text(Math.round(f*10) )
		      			
								
				
				t=2;				
				//$('#menu').css('opacity', '0.2')
				window.setTimeout(function() {ieRotate(-5);}, t*5)
				window.setTimeout(function() {ieRotate(-10);}, t*10)
				window.setTimeout(function() {ieRotate(-15);}, t*15)
				window.setTimeout(function() {ieRotate(-20);}, t*20)
				window.setTimeout(function() {ieRotate(-30);}, t*30)
				window.setTimeout(function() {ieRotate(-40);}, t*40)
				window.setTimeout(function() {ieRotate(-50);}, t*50)
				//$('#menu').css('opacity', '0.3')
				window.setTimeout(function() {ieRotate(-60);}, t*60)
				window.setTimeout(function() {ieRotate(-70);}, t*70)
				window.setTimeout(function() {ieRotate(-80);}, t*80)
				window.setTimeout(function() {ieRotate(-82);}, t*85)
				window.setTimeout(function() {ieRotate(-84);}, t*95)
				window.setTimeout(function() {ieRotate(-86);}, t*100)
				window.setTimeout(function() {ieRotate(-88);}, t*120)
				window.setTimeout(function() {ieRotate(-90);}, t*150)
				//$('#menu').css('opacity', '1')


				
				}
				var toggleMenu = $('.toggle-menu');			
				toggleMenu.animate({right:225}, 300, 'swing')
		
				var rotate = $('.rotate');
				rotate.animate({left:225}, 300, 'swing')

			}
	
	return false;
	} //eo func	
	)

if ($(".id1").length > 0 ){ /* honme page */ 
		$('.content-panel').mouseenter(
			function(){
				$(this).addClass('enter')
			}
		)
		$('.content-panel').mouseleave(
			function(){
				$(this).removeClass('enter')
			}
		)
		
		$('.content-panel').click(
			function(){
			//get link
			var h2=$(this).find('h2')
			var link=h2.find('a')
			
			linkhref=link.attr('href')
			window.location.href=linkhref
			}
		)
	
}	
/* minimise */

	$('.toggle-menu').click(
	function(){
			var $this = $('#menu');
			var inner = $('#menuInner');
			var toggle = $('.toggle-menu');

	if ( $this.hasClass('up')  ){
			if ( $this.hasClass('open') ){
			
				if ($.browser.msie ) { //IE exception
					inner.animate({left:170}, 1000, 'swing')
					$this.find('#mainNav').animate({right:180}, 1000, 'swing')
					$this.find('.intro').delay(300).hide()

				}else{
					$this.animate({top:-170}, 1000, 'swing')
					$this.find('.intro').fadeOut()
					$this.find('#mainNav').animate({right:180}, 1000, 'swing')
				}
				

				$this.removeClass('open')
				toggle.removeClass('open')			
				toggle.find('a').attr('title', 'read more')
				toggle.find('a').text('read more')				
				$("#menu a[title]").tipTip({defaultPosition:'right' });

				$.cookie('introState','1') // set to up closed

			} else {
				if ($.browser.msie ) { //IE exception
					inner.animate({left:0}, 395, 'swing')
					$this.find('#mainNav').animate({right:10}, 395, 'swing')
					$this.find('.intro').show()					
				}else{			
			
					$this.animate( {top:0}, 395, 'swing' )
					$this.find('.intro').fadeIn()
					$this.find('#mainNav').animate({right:10}, 395, 'swing')
				
				}
								
				$this.addClass('open')
				toggle.addClass('open')			
				toggle.find('a').text('hide')				
				toggle.find('a').attr('title', 'hide')			
				$("#menu a[title]").tipTip({defaultPosition:'right' });				
				$.cookie('introState','2') // set to up open
			}

			
	} else{ 
			
			if ( $this.hasClass('open') ){
				$this.animate({left:-170}, 1000, 'swing')						
					if ($.browser.msie ) { //IE exception
					$this.find('.intro').hide()
					}else{
						$this.find('.intro').fadeOut()
					}
				toggle.find('a').text('read more')
				toggle.find('a').attr('title', 'read more')				
				$("#menu a[title]").tipTip({defaultPosition:'right' });				
//				$this.find('#mainNav').animate({right:180}, 1000, 'swing')
				$this.removeClass('open')
				toggle.removeClass('open')				
				$.cookie('introState','4') // set to down closed
				
			} else {
				$this.animate( {left:0} )
				$this.find('.intro').fadeIn()
//				$this.find('#mainNav').animate({right:10}, 500, 'swing')

				$this.addClass('open')
				toggle.addClass('open')			
				toggle.find('a').text('hide')
				toggle.find('a').attr('title', 'hide')				
				$("#menu a[title]").tipTip({defaultPosition:'right' });								
				$.cookie('introState','3') // set to down open
			}
			

	
	}

	return false;
	})



})

/*
1= up closed
2= up open
3= down open
4= down closed
*/
