jQuery(document).ready(function () {
	jQuery('#header-animation').flash({ src: '/swf/header.swf', width: 780, height: 92, flashvars: {autoPlay: true, autoRewind: false}});
	
	jQuery('#nav ul > li').hover(function () {
		jQuery('ul:first', this).fadeIn("fast");
	},
	function () {
		jQuery('ul:first', this).fadeOut("fast");
	});
	jQuery('#nav ul li li').hover(function () {
			jQuery('ul:first', this).each(function () {
				jQuery(this).css('top', jQuery(this).parent().position().top );
				jQuery(this).css('left', jQuery(this).parent().position().left + jQuery(this).parent().width() );
				jQuery(this).show();
			});
		},
		function () {
			jQuery('ul:first', this).hide();
		}
	);
	
	jQuery.getJSON("/getAds.php?size_id=1&random=0", function (data) {
		jQuery.each(data.items, function (i,item) {
			var image = '<img src="' + item.image + '" alt="' + item.alt + '" />';
			if(item.url.length > 0){ image = '<a href="' + item.url + '" title="' + item.title + '">' + image + '</a>'; }
			jQuery("#large-ad").append('<li>' + image + '</li>');
		});
		if(jQuery('#large-ad').children().size() > 1){ 
			jQuery("#large-ad li:first").remove();
			jQuery("#large-ad")
			.before('<div id="ad-nav">')
			.cycle({ 
				fx: 'fade', 
				timeout: 6000,
				pager: '#ad-nav'
			}); 
		}
	});
	
	jQuery.getJSON("/getAds.php?size_id=2&random=1", function (data) {
		var first = true; 
		jQuery.each(data.items, function (i, item) {
			var image = '<img src="' + item.image + '" alt="' + item.alt + '" />';
			if(item.url.length > 0){ image = '<a href="' + item.url + '" title="' + item.title + '">' + image + '</a>'; }
			if(first){
				jQuery(".small-ad:first").append('<li>' + image + '</li>');
				first = false;
			}else{
				jQuery(".small-ad:last").append('<li>' + image + '</li>');
				first = true;
			}
			
			//Grab the href, open it in a window and cancel the click action
			jQuery("a[href^='http']").not("a[href*='northgateshopping.com']").attr('target', '_blank');
		});
		if(jQuery('.small-ad:first').children().size() > 1){ 
			//jQuery(".small-ad:first li:first").remove(); 
			jQuery(".small-ad:first").cycle({ 
				fx: 'fade', 
				timeout: 8000,
				height: '162px'
			});
		}
		if(jQuery('.small-ad:last').children().size() > 1){ 
			//jQuery(".small-ad:last li:first").remove();
			jQuery(".small-ad:last").cycle({ 
				fx: 'fade', 
				timeout: 8000,
				height: '162px'
			});
		}
	});
});