var closeTimer //used for mouseover close events
var multiTimer = []
var menuState = 0
var fontSizes = []
var fontLarge = ""
var loc = document.location + ""
fontSize = 0
fontSizes[2] = "18px"
fontSizes[1] = "16px"
fontSizes[0] = "14px"



$(document).ready(function() { init() })

//main event initilization
function init() {
	$('#searchWrapper input').attr('value','')
	$('#searchWrapper input').click(function(){searchBG()})

	//main menu rollover
	
	//ie6 fix for active subclasses on left nav
	$('.leftNav a.active').each(function() {
		myClass = $(this).attr('class')
		$(this).attr('class', myClass.replace('active', '') + 'Active')
	})

	//ie6 fix for transparencies
	var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
	if (badBrowser) {
		// get all pngs on page
		window.setTimeout(function() {
			$('img[src$=.png]').each(function() {
				fixPng(this);
			});
		}, 1000)
		window.setTimeout(function() {
			$('img[src$=.png]').each(function() {
				
				fixPng(this);
			});
		}, 2000)
	}

	//hide empty print button
	$('img[src$=btnPrint.png]').each(function() {
		if ($(this).parent().attr('href')) {
			if ($(this).parent().attr('href').indexOf('.pdf') < 0) {
				$(this).css('visibility', 'hidden')
			}
		}
	});
 
		
	
	$('.top ul li a img').mouseover(function() {
		var newSRC = $(this).attr('src').replace('.png','_Active.png')
		$(this).attr('src',newSRC)
	})
	$('.top ul li a img').mouseout(function() {
		$(this).attr('src',$(this).attr('src').replace('_Active',''))						 
	})
	
	$('#readMore ul li a img').mouseover(function() {
		var newSRC = $(this).attr('src').replace('.jpg','_Active.jpg')
		$('#readMore ul li a img').addClass('faded')
		$(this).removeClass('faded')
		$(this).attr('src',newSRC)
	})
	$('#readMore ul li a img').mouseout(function() {
		$(this).attr('src',$(this).attr('src').replace('_Active',''))
		$('#readMore ul li a img').removeClass('faded')
	})
	
	if($('#handle').length) {		
		$("#handle").draggable({ axis: 'y', containment: 'parent', drag: function(event, ui) { setScroll() } });	
	}
	

	$('.top ul li a img').each(function() {
		menuPanel(this)								
	})
	
	$('.buttonMenu ul li a img').each(function() {
		buttonPanel(this)
	})

	//mining gold responsibly
	$('.miningMenu ul li a img').mouseover(function() {
		if ($(this).attr('src').indexOf('Large') < 0) {
			var newSRC = $(this).attr('src').replace('.png', '_Active.png')
			$(this).attr('src', newSRC)
		}
	})
	$('.miningMenu ul li a img').mouseout(function() {
		$(this).attr('src', $(this).attr('src').replace('_Active', ''))
	})

	$('.miningFlow ul li a img').mouseover(function() {
		var newSRC = $(this).attr('src').replace('.png', '_Active.png')
		$(this).attr('src', newSRC)
	})
	$('.miningFlow ul li a img').mouseout(function() {
		$(this).attr('src', $(this).attr('src').replace('_Active', ''))
	})
		

	if ($('.bluebox').length) {
		boxTops()
	}

	if ($('.leftPractices').length || $('.leftOurStrategy').length || $('.leftPracticesActive').length || $('.leftOurStrategyActive').length) {
		nav4()	
	}
	
	checkFontSize()
	checkCaseStudy()
	
}


var blank = new Image();
blank.src = 'images/blank.gif';

function fixPng(png) {
	var src = $(png).attr('src');
	if (src.indexOf('miningMenu') > -1) {
		return
	}
	
	//alert(png.width+", "+png.height+", " + src)
	if (!png.style.width) {
		$(png).css('width', png.width + 'px')
	}
	if (!png.style.height) {
		$(png).css('height', png.height+ 'px')
	}
	png.src = blank.src;
	png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}



//utility functions - common library
function findPos(obj) {
	//returns x,y coordinates of an element
	var curleft = 0;
	var curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function closeInit(element) {
	//timer for mouseout on panels
	clearTimeout(closeTimer)
	closeTimer = window.setTimeout(function(){closePanel(element)},300)
}

function closeMulti(src,element) {
	//timer for mouseout on panels
	clearTimeout(multiTimer[src])
	multiTimer[src] = window.setTimeout(function() { $(element).hide()}, 300)
}


function closePanel(element) {

	$(element).fadeOut('fast');
}

function mousePanel(src,panel,initFunction) {
	$(src).mouseover(function(){window[initFunction](src)})
	$(src).mouseout(function(){closeInit(panel)})
	$(panel).mouseover(function(){clearTimeout(closeTimer)})
	$(panel).mouseout(function(){closeInit(panel)})
}

function openDialog(element) {
	//this is the general dialog handler.
	//pass the element name and this will copy
	//the contents of the element to the dialog box

	$('#overlayGreen').css('height',$(document.body).height()+'px')
	$('#overlayGreen').show()	
	$('#dialog').html($(element).html())
	centerMe('#dialog')
	$('#dialog').show();
}

function closeDialog() {
	$('#overlayGreen').hide()
	$('#dialog').hide()
	$('#dialog').html('')
}

function centerMe(element) {
	//pass element name to be centered on screen
	var pWidth = $(window).width();
	var pTop = $(window).scrollTop()
	var eWidth = $(element).width()
	var height = $(element).height()
	$(element).css('top','130px')
	//$(element).css('top',pTop+100+'px')
	$(element).css('left',parseInt((pWidth/2)-(eWidth/2))+'px')
}

function setCookie(c_name, value, expiredays) {
	var exdate = new Date()
	exdate.setDate(exdate.getDate() + expiredays)
	document.cookie = c_name + "=" + escape(value) +
	((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
}

function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=")
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1
			c_end = document.cookie.indexOf(";", c_start)
			if (c_end == -1) c_end = document.cookie.length
			return unescape(document.cookie.substring(c_start, c_end))
		}
	}
	return ""
}


// main functions

function searchBG() {
	$('#searchWrapper').addClass('active')	
}

function menuPanel(src) {
	//top menu rollover
	$(src).mouseover(function() {
		if (menuState == 0) {
			menuState = 1
			$('.menuMain ul li ul').hide();			
			$(this).parent().next().slideDown('fast', function() { menuState = 0; })
		}

	})
	$(src).mouseout(function() { menuState = 0; closeMulti(src, $(this).parent().next()) })
	$(src).parent().next().mouseover(function(){
	
		$(src).parent().addClass('active');
		clearTimeout(multiTimer[src])
		if($(src).attr('src').indexOf('_Active')<0) {
			var newSRC = $(src).attr('src').replace('.png','_Active.png')
			$(src).attr('src',newSRC)
		}
	})
	$(src).parent().next().mouseout(function(){
		$(src).attr('src',$(src).attr('src').replace('_Active',''))
		closeMulti(src,$(this))
	 })
}


function buttonPanel(element) {
	if ($(element).parent().next('div').length<1) {
		return
	}

	//utility button rollover
	$(element).mouseover(function() { $('.buttonPanel ul li div').hide(); $(this).parent().next().slideDown('fast') })
	//$(src).mouseout(function() { closeMulti(src,$(this).parent().next()) })
	$(element).mouseout(function() { closeInit($(this).parent().next()) })
	$(element).parent().next().mouseover(function() { clearTimeout(closeTimer) })
	$(element).parent().next().mouseout(function() { closeInit($(this)) })

}



function setScroll() {
    //scrollbar handler for notes
    var myTop = parseInt($('#handle').css('top').replace('px', '')) 
    var trackHeight = $('#sliderTrack').height()
    var sliderPos = myTop / trackHeight*1.08
    var contentHeight = $('.scrollContent').height()
    newTop = parseInt(sliderPos * (trackHeight - contentHeight))+'px'
    $('.scrollPanel').children().css("top", newTop)

 }

function fixPath() {
	$('img').each(function() {
	$(this).attr('src', $(this).attr('src').replace("/Assets", "http://kinross.beta.moveable.ca/Assets"))
	})
}




function setFontSize(n) {
	fontSize++
	if (fontSize > 2) {
		fontSize = 0
	}
	setCookie('font', fontSize, 1)
	changeFontSize()
	fSize = getCookie('font')
}


function checkFontSize(n) {
	fontSize = getCookie('font')
	if (!isNaN(fontSize)) {
		changeFontSize()		
	}
}

function changeFontSize() {
	$('#contentMain p').css('font-size', fontSizes[fontSize])
	$('#contentMain li').css('font-size', fontSizes[fontSize])
	$('#contentMain .callout').css('font-size', fontSizes[fontSize])
	$('.caseLeft p').css('font-size', fontSizes[fontSize])
	$('.caseLeft p').css('font-size', fontSizes[fontSize])
	$('#contentMain table').css('font-size', fontSizes[fontSize])
	$('.caseLeft').css('font-size', fontSizes[fontSize])
	$('.directors2 li').css('font-size','11px')
}

function checkCaseStudy() {

	if ($('#highlights').length) {
		sHeight = $('#highlights .scrollPanel').height()
		cHeight = $('#highlights .scrollContent').height()
		//alert(cHeight + "," + sHeight)
		if (cHeight == sHeight) {
			$('#sliderTrack').addClass('hidden')
		}
	}
	if (loc.indexOf('caseid=6') > 0) {
		$('.case').addClass('paracatu')
		var tmpImg = $('.caseRight img').attr('src')
		$('.caseRight').remove()
		$("<img class='caseImgSpecial' src='"+tmpImg+"'>").insertBefore('.caseLeft h2')
	}
	
}

function boxTops() {
	$('.bluebox').each(function(){
		$(this).html("<div class='boxTop'></div>" + $(this).html())
	})
}

function nav4() {
	var locTemp = loc.split("/")
	locTemp = locTemp[locTemp.length - 1]
	locTemp = locTemp.replace(".aspx", "")
	if($("#"+locTemp)) {
		if (locTemp == 'Our_Strategy') {
			$('.leftOurStrategy').after("<div class='sub4'>" + $('#Our_Strategy').html() + "</div>")
			return;
		}
		$("#" + locTemp).show()		
	}
}

function setGold(section) {
	$('.gold').hide()	
	$("#Sec_"+section).show()	
}

function hideLightbox() {
	$('#overlay').hide()
	$('#lightbox').hide()
	$('#lightbox').html('')
}

function lightbox(contents) {
	//pass it html
	//example: lightbox($('#divName').html())
	$('#overlay').show()
	$('#lightbox').css('top', $('#root').scrollTop()+50+ 'px')
	$('#lightbox').css('left', '-4400px')
	$('#lightbox').html(contents)
	var newLeft = ($(window).width() / 2) - ($('#lightbox').width() / 2)
	$('#lightbox').css('left', newLeft+'px')
	$('#lightbox').show()
}


