$(document).ready(function() {
    
    //Some global vars to make edits nice and easy
    Leo.contacts = $('.location-select');
    Leo.active = 'active';
    Leo.countryList = 'country-list';
    Leo.officeInfo = 'office-info';
    Leo.contactInfo = 'contact-info';
    //window.addEventListener("orientationchange", Leo.hideURLBar, false);
    //Check device and deliver appropriate content
   
    Leo.regionClick();
    Leo.countryClick();
    Leo.MediaQuerySWF('#flashContent', 'main.swf', 'flashMovie');

    
});

//Declare Leo namespce
var Leo = {};

//Click event for the region list items, calls Leo.toggle
Leo.regionClick = function() {
    Leo.contacts.children('li').click(function() {
        $(this).find('.' + Leo.countryList).children('li').removeClass('active');
        $('.' + Leo.officeInfo).stop(true, true).slideUp(300);
        Leo.toggle(this, '.' + Leo.countryList);
    });
};

//Click event for the country list items, calls Leo.toggle
Leo.countryClick = function(){
    $('.'+Leo.countryList).children('li').click(function(e){
        e.stopImmediatePropagation();
        Leo.toggle(this, '.'+Leo.officeInfo);
    });
};

//This function handles the toggle event for the region and countries click events
Leo.toggle = function(element, list){
    $(element).siblings().find(list).stop(true, true).slideUp(300);
    $(element).siblings().removeClass('active');
    $(element).find(list).is(':visible') ? $(element).removeClass('active') : $(element).addClass('active');
    $(element).find(list).stop(true, true).slideToggle(300);
};


//This loads the swfobject
Leo.LoadSWF = function (swf, elementId, width, height, version) {
    var flashobject = swf;
    var expressinstall = 'expressInstall.swf';
	var flashvars = {};
	var params = {
	    quality: "high",
	    play: "true",
	    loop: "false",
	    scale: "noscale",
	    wmode: "window",
	    devicefont: "false",
	    bgcolor: "#cccccc",
	    menu: "true",
	    allowfullscreen: "false",
	    allowscriptaccess: "sameDomain"
	};
	var attributes = {
	    id: "main",
	    name: "main"
	};

    swfobject.embedSWF(flashobject, elementId, width, height, version, expressinstall, null, params, attributes);
	swffit.fit("main", 1000, 666);
};

//Hides the SWF from mobile browsers that run Flash (android) -- used in conjunction with css media query
//Still needs to be implemented!
Leo.MediaQuerySWF = function (element, swf, movieContain) {
	var display = $(element).css('display');
	if (display == 'none') {
		$(element).css('display', 'block');
		$(element).css('width', '100%');
		$(element).css('height', '100%');
	} else if (document.documentElement.clientWidth <= 480) {
		$(element).css('display', 'block');
		$(element).css('width', '100%');
		$(element).css('height', '100%');
	} else {
		Leo.LoadSWF(swf, movieContain, '100%', '100%', '10.0.0'); // use this to load swf instead of whatever's currently being used?
	}
};

// Hide the navigation bar
Leo.hideURLBar = function() {
	window.scrollTo(0, 1);
};

// Legacy code from common.js
var openWin;
function newWin(popUrl){
		var openWin = window.open(popUrl,'newPopup','menubar=no,scrollbars=yes,resizable=no,width=560,height=444');
		openWin.focus();
}

