var stupidBrowser = false
var selectedBranch = ''
var currentBranch = ''

$(function(){	
	determineBrowser()
	if(!stupidBrowser) {
		roundCorners()
	}
	else {
		$('#normalContent .bigButton.standard').each(function(){
			try {
				$(this).css('border','1px solid grey')
			}
			catch (err) {}
		})
		$('#contactTable td').each(function(){
			$(this).css('text-align','left').attr('align','left')
		})
		$('#googleFrame').hide()
		$('#googleImage').css({
			'height': '200px',
			'width': '400px',
			'position': 'absolute',
			'top': '20px',
			'right': '20px'
		}).attr('width',500).show()
	}
	initialiseFont()
	initialiseGallery()
	displayUI()
})

function determineBrowser() {
	if($.browser.msie && ($.browser.version=="6.0" || $.browser.version=="5.0" || $.browser.version=="5.5" || $.browser.version=="7.0")){
		stupidBrowser = true
		$('#incompatibleAlert').show()
	}
}

function highlightObj(cssRef) {
	$(cssRef).effect("highlight", {}, 4000);
}

function roundCorners () {
	if ($.support.borderRadius && !stupidBrowser) {
		$('#header').corner('8px tr tl')
		$('#footer').corner('8px br bl')
		$('#navBar').corner('18px tl')
		$('#contactTable input, #contactTable textarea, #contactTable select').corner('5px')
		$('.bigButton.standard').corner('18px tl br')
	}
}

function validEmail (emailStr) {
	var pattern = new RegExp(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)
	return pattern.test(emailStr)
}

function initialiseFont() {
	$('h1, h2, .fancy, a.contentLink, h3').each(function(){
		$(this).text($(this).text().replace(/(^|\s)([a-z])/g,function(m,p1,p2){return p1+p2.toUpperCase()}))
		Cufon.replace($(this))
	})
}

function initialiseGallery() {
	$('#gallery').cycle({
		fx: 'fade',
		fit: 1,
		timeout: 5000
	});
	$('#imageSelect img').hover(function(){
		$(this).animate({
			opacity: 0.2
		}, 500)
		$('#gallery').cycle(parseInt($(this).attr('index')))
	}, function(){
		$(this).animate({
			opacity: 1
		}, 500)
	})
}

function displayUI() {
	if(!stupidBrowser){
		$(".bigButton").css('opacity',0)
		setTimeout('$(".bigButton").animate({"opacity": 1},2000)',1000)
	}
	// IE7 compatibility
	if($.browser.msie && $.browser.version.substr(0, 1) == '7') {
		$('#navBar').css('margin-top','-40px')
		//$('#contactButton').css('margin-left','750px')
	}
	$('#contactButton').button()//.css('font-size','13px')
}

function navLink(URL) {
	if(selectedBranch != currentBranch) {
		var container = $('#bottomContent')
		var nContent = $('#normalContent')
		var sContent = $('#solutionsContent')
		var aContent = $('#adviceContent')		
		$(nContent,sContent,aContent).each(function(){
			if(!stupidBrowser) {
				$('#normalContent, #solutionsContent, #adviceContent').fadeOut()
			}
			else {
				$('#normalContent, #solutionsContent, #adviceContent').hide()
			}
		})
		switch(selectedBranch) {
			case 'solutions':			
				var dynHeight = '100px'
				var showContent = $(sContent)
				$('#mainContent').css('height','450px')
				$(container).show()
				break
			case 'advice':
				var dynHeight = '120px'
				var showContent = $(aContent)
				$('#mainContent').css('height','450px')
				$(container).show()
				break
			case 'newsletter':
				var dynHeight = '180px'
				var showContent	
				$(container).hide()
				$('#mainContent').css('height',parseInt(450+parseInt(dynHeight.replace('px','')))+'px')
				break
			default:
				var dynHeight = '180px'
				$('#mainContent').css('height','450px')
				var showContent = $(nContent)
				$(container).show()
		}
		if(!stupidBrowser) {
			if(selectedBranch != 'newsletter'){
				$(container).animate({
					height: dynHeight
				}, 1500, function() {				
					//setTimeout("$('#"+$(showContent).attr('id')+"').fadeIn(1000)",1000)
					setTimeout("$('#"+$(showContent).attr('id')+"').fadeIn(1000)",500)
				})	
			}
			else {
				setTimeout("$('#"+$(showContent).attr('id')+"').fadeIn(1000)",500)
			}
		}
		else {
			$(container).css('height',dynHeight)
			if(selectedBranch != 'newsletter'){
				$(showContent).show()
			}
		}
	}
	URL = URL.toLowerCase()
	var URLarr = URL.split('.')
	if(URLarr[URLarr.length-1].toLowerCase() == 'pdf'){
		window.open(URL)
		iframeTransition('#mainContent','content/pdf-notification.html?'+URL)
	}
	else{
		iframeTransition('#mainContent',URL)
	}
	currentBranch = selectedBranch
}

// Publically defined as the iframeTransition function has a setTimeout to change the src of the iframe
	var iframeObj

function iframeTransition(iframeCSSref,URL) {
	iframeObj = eval('$("'+iframeCSSref+'")')
	$('#loadingDiv').show()
	if(stupidBrowser){
		$(iframeObj).css('opacity', 0)
		$(iframeObj).attr("src",URL)
		$(iframeObj).unbind('load')
		$(iframeObj).load(function(){
			$('#loadingDiv').hide()
			$(iframeObj).css("opacity", 1)
		})
	}
	else {
		$(iframeObj).animate({"opacity": 0},1000)
		setTimeout('$(iframeObj).attr("src","'+URL+'")',1000)
		$(iframeObj).unbind('load')
		$(iframeObj).load(function(){
			$('#loadingDiv').hide()
			$(iframeObj).animate({"opacity": 1},2000)
		})
	}
}

function newsletterLink(topicIndex) {
	var topicURL = window.location.toString().split('/content/news')[0]+'/content/news/content/'+topicIndex+'.html'
	iframeTransition("iframe.newsletterIframe",topicURL)	
}

function loansLink(topicIndex) {
	var topicURL = window.location.toString().split('/content/services/loans')[0]+'/content/services/loans/content/'+topicIndex+'.html'
	iframeTransition("iframe.loansIframe",topicURL)	
}
