// FOODBANKS Global Object
 if (typeof FOODBANKS == "undefined" || !FOODBANKS) {
    var FOODBANKS = {};
}


// Utility classes
FOODBANKS.Util = {
	isCMSEditMasterPage: function() {
		var url = location.pathname;
		var urlParts = url.split('/');
		if(($.inArray('cms', urlParts) > 0) && ($.inArray('default.aspx', urlParts) > 0)) {
			return true;
		}
		else {
			return false;
		}
	}
}

// MegaNav
FOODBANKS.MegaNav = {
	init: function() {
		// Before initializing the megaNav, make sure we are not editing the CMS master page
		if(FOODBANKS.Util.isCMSEditMasterPage() == false) {
			var myClassName;
			// Set up hoverIntent config object
			
            var hoverConfig = {
				over: function() {
					myClassName = $(this).attr('class');
                                        //console.log(myClassName);
					if(myClassName != "") {
						$(this).append($("div#megaNav div."+myClassName));
						$(this).children("div.megaNavPanel").hide();
						$(this).children("div.megaNavPanel").fadeIn('fast');
					}
				},
				out: function() {
					$(this).children("div.megaNavPanel").fadeOut('fast', function() {
						$("div#megaNav").append($(this).children("div.megaNavPanel"));
					}); 
				}, 
				timeout: 250
			};
			$("ul#menuElem li").hoverIntent(hoverConfig);
                        
		}
		// If we are editing the CMS master page, then make the meganav blocks visible so we can edit them
		else {
			$("div#megaNav div.megaNavPanel").show();
		}
	},
	iOSEventHandlers: function() {
		// Handles click events for iOS mobile devices
		// Script stolen from: http://stackoverflow.com/questions/3038898/ipad-iphone-hover-problem-causes-the-user-to-double-click-a-link
		$('a').live('touchend', function(e) {
		    var el = $(this);
		    var link = el.attr('href');
		    window.location = link;
		});

	}
}

FOODBANKS.CodaSlider = {
	init: function() {
		$('#home-slider').codaSlider({
            autoSlide: true,
            autoSlideInterval: 7000,
            dynamicArrows: false,
            dynamicTabs: true
    	});
	}
}

$(document).ready(function(){
    $("a[rel='press-release-gallery-image']").colorbox();

    $(".video-lightbox").colorbox({opacity: 0.9, iframe:true, innerWidth:640, innerHeight:510});
    
    $("#click").click(function(){ 
        $('#click').css({"background-color":"#f00", "color":"#fff", "cursor":"inherit"}).text("Open this window again and this message will still be here.");
        return false;
    });
    
    // Initialize the CodaSlider
	FOODBANKS.CodaSlider.init();
   
   	// Initialize the MegaNav
   	FOODBANKS.MegaNav.init();
	if(navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
    	FOODBANKS.MegaNav.iOSEventHandlers();
	}
	           
});

function openPDF() {
   window.open("http://www.foodbankscanada.ca/FoodBanks/PDF/PDFGenerator.aspx", "GeneratePDF");
}
function openPDF2() {
   window.open("/FoodBanks/PDF/PDF.aspx?fn=print", "GeneratePDF");
}
