var base_width = 1150;
var base_height = 960;
var proportion = base_width / base_height;
var width = base_width;
var height = base_height;
var currentImg = 1;
var time = 0;
var timeout = "";
var stopTimer = "n";
var currentImage = 0;
var timerSwitch = 0;
var counter = 1;
var counter2 = 2;

jQuery(document).ready(function($) {
    setEmails();

	// PNG Fix
	if($.browser.msie && $.browser.version == '6.0'){
		DD_belatedPNG.fix('div#header-main, div#footer-main, ul li img, div.tout, div.tout-home-right, div#search-body, div#search-left, input#subSearch, div#logo h1, div#main, div#top-nav ul li a, div#content-image div.toggle ul li a, div.tout-right ul li');
	}
	
	// Rounded corners Fix
	//if($.browser.msie && ($.browser.version == '7.0' || $.browser.version == '6.0')){
	//	DD_roundies.addRule('div#search', '4px', false);
	//}
  
  	//Tout rollovers
	//div.tout-home-right
	$('div.tout').hover(
		function()
		{
			$(this).find('a.arrow').css('background','transparent url(common/images/arrow-over.gif) right bottom no-repeat');	
		},
		function()
		{
			$(this).find('a.arrow').css('background','transparent url(common/images/arrow.gif) right bottom no-repeat');	
		}
	);
	
	incTime();

	var width = $(window).width();
	var height = $(window).height();

	//manageBackground(width,height);
	//manageBackgroundDiv();
	
	//$('div.footer-overflow').height(($(window).height() - 865));

	$(window).resize(function(){
	//	$('div.footer-overflow').height(($(window).height() - 865));
	
	});
	
	//	width = $(window).width();
	//	height = $(window).height();
		
	//	manageBackground(width,height);
	//	manageBackgroundDiv();

	$('div.toggle ul li:nth-child(1)').addClass('active');

	$('div#content-image div.toggle ul li').hover(function(){
		stopTimer = "y";
			 
		$('div.toggle ul li').removeClass('active');
		if($(this).attr('class') != ('toggle-' + currentImg)){
			$img = $(this).attr('class').replace('toggle-','');
			currentImg = $img;
			$('div#content-image div.image').fadeOut(100);
			$('div#content-image div.img-' + $img).fadeIn(100);
			$(this).addClass('active');
		}
		if($(this).attr('class') == 'toggle-' + currentImg){
			$(this).addClass('active');
		}
	}, function(){
		if($(this).text() == 1){
			time = 0;
		}
		else if($(this).text() == 2){
			time = 6;
		}
		else if($(this).text() == 3){
			time = 11;
		}
		stopTimer = "n";
		//counter++;
		//if(counter < 1){
			//incTime();
		//}
	});
});

function incTime(){
len = $('div#content-image div.image').length;
//console.log('len: ' + $('div#content-image div.image').length)

	if(stopTimer == "n"){
		time = time ++;
		timeout=setTimeout("incTime()", 2600);
		
		if ((time % 10) == 0){

	        if (counter2 > len)
	            counter2 = 1;
        
        	if (counter > len)
        	    counter = 1;


		    imgSwitch(counter2,counter)
		    counter ++;
		    counter2 ++;
		}

	}
}

function imgSwitch(img,imgNext){
	var method1 = 'easeOutCirc';
	var method2 = 'easeInCirc';
	$('div#content-image div.image').fadeOut(400, method1);
	$('div#content-image div.img-' + img).fadeIn(400, method2);
	$('div.toggle ul li:nth-child(' + imgNext + ')').removeClass('active');
	$('div.toggle ul li:nth-child(' + img + ')').addClass('active');
	currentImg = img;
}

function manageBackgroundDiv()
{
	$footerMain = $('div#footer-main');
	$mainMain = $('div#main-main');
	$main = $('div#main');
	$footer = $('div#footer');
	$headerMain = $('div#header-main');
	$header = $('div#header');
	$backgroundDiv = $('div#background');
	
	$winHeight = $(window).height();
	$offsetFooter = $footerMain.offset();
	$footerPlus = $offsetFooter.top + 100;
	
	//$backgroundDiv.height($winHeight);
	$backgroundDiv.css('overflow','hidden');
}

function manageBackground(wid,hi) {

	if((hi > wid) || (wid/proportion < hi)) {
		
		width = hi*proportion;

		height = hi;
			
		if(wid<=960) {
			
			width = 960;
			height = width / proportion;
			
			if(hi > height) {
				
				var cssObj = {
					'left' : 0,
					'top' : 0,
					'margin-left' : 0,
					'margin-top' : 0
				}
				
			} else {
				
				var cssObj = {
					'left' : 0,
					'top' : "50%",
					'margin-left' : 0,
					'margin-top' : -200
				}

			}
			
		} else {
			
			var cssObj = {
				'left' : "50%",
				'top' :0,
				'margin-left' : -(width)/2,
				'margin-top' : 0
			}
			
		}

		$("#background img").attr("width",width);
		$("#background img").attr("height",height);
		
		$("#background").css(cssObj);
		
	} else {
		
		if(wid<=960) {
			
			width = 960;
			height = width/proportion;
			
			if(hi > height) {
				
				var cssObj = {
					'left' : 0,
					'top' :0,
					'margin-left' : 0,
					'margin-top' : 0
				}
				
			} else {
				
				var cssObj = {
					'left' : 0,
					'top' :"50%",
					'margin-left' : 0,
					'margin-top' : -200
				}
			
			}
			
		} else {
			
			width = wid;
			height = wid/proportion;
			
			var cssObj = {
				'left' : 0,
				'top' : "50%",
				'margin-left' : 0,
				'margin-top' : -200
			}
			
			
		}
		
		$("#background img").attr("width",width);
		$("#background img").attr("height",height);
		
		$("#background").css(cssObj);
	
	}
	
}


function setEmails(){
    var emailLinks = $('li.email').find('a');

    for(i=0; i<emailLinks.length; i++){
        $('li.email a').click(function(){
            mailpage();
            return false;
        });
    }
}


function mailpage()
{
    var pageTitle = document.title;
    var pageURL = location.href;
    pageTitle = pageTitle.replace("&", "%26");
    pageURL = pageURL.replace("&", "%26");

    mail_str = "mailto:?subject= www.reefsubsea.com " + pageTitle;
    mail_str += "&body=Have a look at the this page on the Reef Subsea website: " + pageURL;
    //mail_str += "%0D%0A"; //line breaks

    location.href = mail_str;
}
 
