function hasFlash(e) {
	$('html').addClass('has-flash')
}
var resizeFlash = {
	"winH" : 0,
	"newH" : 0,
	"setup" : function() {
		resizeFlash.win = $(window);
		resizeFlash.cont = $('#container');
		resizeFlash.resize();
	},
	"resize" : function() {
		resizeFlash.winH = resizeFlash.win.height() - 115;
		resizeFlash.newH = resizeFlash.cont.width() / 16 * 9;
		resizeFlash.newH = Math.min(resizeFlash.newH, resizeFlash.winH);
		resizeFlash.newH = Math.max(resizeFlash.newH, 605);
		resizeFlash.cont.css({height : resizeFlash.newH + "px"});
	}

};
function resizeContainer() {
/*	var hgtFlash = $(window).height() - ($('#header').height() + $('#footer').height());
	var hgtContainer = $('#container');
	console.log(hgtFlash, hgtContainer);
	var hgtBrowserOffset = 0;
	if (hgtFlash > 605) {
		
			/*if ( $.browser.msie ) {
				
				hgtFlash = hgtFlash + 25;
  				$('#full #container').attr('style', 'height:'+hgtFlash+'px; background:#000;');
				
			} else if ( $.browser.mozilla) {
				//alert('firefox')
				hgtFlash = hgtFlash + 30;
  				$('#full #container').attr('style', 'height:'+hgtFlash+'px; background:#000;');
			
			} else {
				//alert('chrome, safari')
				hgtFlash = hgtFlash - 10;
				$('#full #container').attr('style', 'height:'+hgtFlash+'px; background:#000;');
			}*/
			/*if ( $.browser.msie ) {
				hgtBrowserOffset = 25;
				
			} else if ( $.browser.mozilla) {
				hgtBrowserOffset =  30;
			
			} else {
				hgtBrowserOffset = -10;
			}
	}
	hgtContainer.css({"height" : (hgtFlash + hgtBrowserOffset) + "px", "background" : "#000"})*/
}

function getFlashMovie(movieName)
{
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    return (isIE) ? window[movieName] : document[movieName];
}


$(document).ready(function(){
	
	// GF - not necessary in all pages therefore have moved it to header.php where it can be conditionally called upon based on the page being viewed.	
	// resizeFlash.setup();

	// $(window).resize(function() {
	// 	resizeFlash.resize();
	// });

	if (window.menuDisabled) {
		$('#nav').addClass('disabled');
	}
	// JS is enabled 
	$("body").addClass('has-js');
	
	// Language dropdown
	$("#language").hover(
		function() {
			$(this).find("ul li").hide();
			$(this).find("ul").filter(':not(:animated)').slideDown(400, function() {
				$(this).children('li').fadeIn(400);
			});
		},
		function() {
			$(this).children('li').fadeOut(100);
			$(this).find("ul").filter(':not(:animated)').slideUp(400);
		}
	);
	
	// Hide sub nav 
	$("#header ul li ul").hide();
	
	$('#header-background').slideUp(0);
	
	// Main nav hover
	$("#nav").hoverIntent(
		function() {
			
			if (window.menuDisabled) {
				if (swfobject.getObjectById("Flash")) {
					swfobject.getObjectById("Flash").showMenu();
				}
			}
			else {
				$('#nav ul').stop(true, true).delay(300).fadeIn(200);
				$('#header-background').stop(true, true).slideDown(350);
			}

		},
		function() {

			if (window.menuDisabled) {
				if (swfobject.getObjectById("Flash")) {
					swfobject.getObjectById("Flash").hideMenu();
				}
			}
			else {
				$('#nav ul').stop(true, true).fadeOut(250);
				$('#header-background').delay(200).slideUp(300);
			}

		}
	);
	
	// Get all immediate child divs and their count
	var $history_sections = $('#content-history > li');
	var pages = $history_sections.length;
	
	// Hide every one but the first
	$history_sections.not(':first').hide();

	
	// Resize every item to the tallest one
	// RICH: Removed this code as it broke IE6's page rendering, and content in history not pushing beyong minimum size
	// anyway so I think we're safe with content we have
	
	//var max = 0;
	//$('#content-history li').each(function () {
	//	if ($(this).height() > max) {
	//		max = $(this).height();
	//	}
	//}).height(324);
	
	
	
	// Create a container for the pagination
	var currentIndex = 0;
	$ul = $('<ul />');

	// Create links for pagination inside the ul
	for(var i = 0; i < pages; i++) {
	    $ul.append($('<li><a href="#" rel="'+(i)+'"></a></li>'));
	}
	
	// Insert the pagination container and next / prev links
	$('div.pagination').append($ul);
	$('.pagination ul').before($('<div id="history-prev" class="disabled"><a href="javascript:void(0);" class="arrow"><span>prev</span></a></div>'));
	$('.pagination ul').after($('<div id="history-next"><a href="javascript:void(0);" class="arrow"><span>next</span></a></div>'));
	$('.pagination ul').find('a:eq(0)').parent().addClass('selected');

	// Behaviour for clicking the links inside pagination container
	$('.pagination ul').find('a').click(function() {
						
		// Get the index from current element's rel attribute
		currentIndex = parseInt($(this).attr('rel'), 10);
		
		// || currentIndex == pages - 1
		if (currentIndex == 0) {	
			$('#history-prev').addClass('disabled');
		} else {
			$('#history-prev').removeClass('disabled');
		}
		if (currentIndex == pages - 1) {
			$('#history-next').addClass('disabled');
		} else {
			$('#history-next').removeClass('disabled');
		}
		
		showHistoryPage(currentIndex, $(this));
		
	    return false;
	});
	
	
	// Behaviour for clicking the next / prev links
	$('.pagination').find('a.arrow').click(function () {
		
		var id = $(this).parent().attr('id');
		
		if (id == 'history-prev' && currentIndex > 0) {
			currentIndex--;
			
		}
		else if (id == 'history-next' && currentIndex < pages - 1) {
			currentIndex++;
			
		}
		
		$('.pagination').find('a.arrow').parent().removeClass('disabled');
		
		if (currentIndex == 0 || currentIndex == pages - 1) {
			
			$(this).parent().addClass('disabled');
		}
		
		showHistoryPage(currentIndex, $(this));
	
	});
	
	
	
	
	function showHistoryPage(index, _this) {

		// Remove any selected class
		$('.pagination li.selected').removeAttr('class');
		
		// Show selected pagination item
		$(_this).parent().parent().find('li').eq(index).addClass('selected');
		
		// Hide every div and show the div at the current index's location
		$history_sections.hide().eq(index).show();
		
		// Find active year
		var current_year = $history_sections.eq(index).find('h3').html();
		
	}
	
	// Contact form 
	function getContactInfo(){
		
		// Store dropdown list value
		var myval=$("#country").val();
		
		if (myval == 0){
			$('#address').html("<h4>WRANGLER EUROPE</h4><p>Fountain business park<br/>C. van kerckhovenstraat 110<br/>Building 2<br/>B-2280 Bornem</p><p>Belgium</p><p>Tel. +32 3 298 2000<br/>Fax. +32 3 298 2002</p>")
		}
		else{
			$.post("http://" + window.location.hostname + "/contact_details.php", { scriptCharset: "utf-8" ,"contact_code": ""+myval+""},
			function(data){
				$('#address').html('<p>' + data.address + '</p>' + '<p>Email. ' + data.email + '</p>'+ '<p>Tel. ' + data.phone + '<br/>Fax. ' + data.fax + '</p>');
				}, "json");
			}
	}
	
	// Press form 
	function getPressInfo(){
		
		// Store dropdown list value
		var myval=$("#country").val();
		
		if (myval == 0){
			$('#address').html("<h4>WRANGLER EUROPE</h4><p>Fountain business park<br/>C. van kerckhovenstraat 110<br/>Building 2<br/>B-2280 Bornem</p><p>Belgium</p><p>Tel. +32 3 298 2000<br/>Fax. +32 3 298 2002</p>")
		}
		else{
			$.post("http://" + window.location.hostname + "/press_details.php", { scriptCharset: "utf-8" ,"contact_code": ""+myval+""},
				function(data){
					var html = '';
					
					if (data.name) {
						html += '<p>' + data.name + '</p>';
					}
					if (data.address) {
						html += '<p>' + data.address + '</p>';
					}
					html += '<p>';
					
					if (data.email) {
						html += 'Email. ' + data.email + '<br />';
					}
					if (data.tel) {
						html += 'Tel. ' + data.tel + '<br />';
					}
					if (data.web) {
						html += 'Web. ' + data.web + '';
					}
					html += '</p>';
					$('#address').html(html);
			}, "json");
		}
	}
		
	$("#contact #country").change(function() {
		getContactInfo();
	});
	$("#press #country").change(function() {
		getPressInfo();
	});
	
	
	//call external links function	
	CORE.externalLinks();
	CORE.relExternalLinkStyle();
	
});// end doc.ready


////////////////////////////////////////////////////////////////
//                        Sharing
////////////////////////////////////////////////////////////////
function popup(url, name, width, height) {
	var left   = (screen.width  - width)/2;
	var top    = (screen.height - height)/2;
	var params = 'width='+width+', height='+height;
	params += ', top='+top+', left='+left;
	params += ', directories=no';
	params += ', location=no';
	params += ', menubar=no';
	params += ', resizable=no';
	params += ', scrollbars=no';
	params += ', status=no';
	params += ', toolbar=no';
	newwin=window.open(url, name, params);
	if (window.focus) {newwin.focus()}
	return false;
}

function shareTwitter() {
	if(window.location.href.indexOf("blue-bell") != -1)
	{
		popup("http://twitter.com/share?url=http://bit.ly/hqgP4S&text=Hack, cut and grind your way through the Bluebell FW 11 collection - URL", "shareTwitterBB", 550, 450);
	}
	else
	{
		if(window.location.href.indexOf("we-are-animals") != -1)
		{
			popup("http://twitter.com/share?url=http://bit.ly/i59KJe&text=Be the We Are Animals stuntman. Feel what they feel, and see what they see in this new interactive piece from Wrangler.", "shareTwitterBB", 550, 450);
		}
		else
		{
			popup("http://twitter.com/share?url=http://bit.ly/hkn92l&text=Mark your territory with the Wrangler FW11 collection", "shareTwitter", 550, 450);
		}
	}
}

// NB Meta tags for FB share go in HEAD
function shareFacebook() {
	popup("http://www.facebook.com/share.php?u=" + window.location.href, "shareFacebook", 550, 450);
}

function shareGalleryTwitter() {
	popup("http://twitter.com/share?url=http://bit.ly/icxDaz&text=Wrangler presents STUNT: Hollywood daredevils on fire, bursting through windows, getting blown up...", "shareTwitterBB", 550, 450);
	
}



var CORE = {

/*==========================================
	External links
============================================*/

	 externalLinks: function(){
		 
		$('a[rel="external"]').click( function(e){
			var _externalLinks = this;
			window.open( $(_externalLinks).attr('href') );																
			e.preventDefault();
		});

	},//end externalLinks
	
	relExternalLinkStyle: function(){

		$('a[rel=external]').css({'paddingRight':'16px','background': 'url( "http://'+document.domain+'/wp-content/themes/wrangler/images/icon_external-link.png") right 4px no-repeat'});
		}
	
}// end CORE


function trackEvent(value, category){
	_gaq.push(['_trackEvent', category, value]);
}
