/**
 * Global Onload Method
 * 
 * currently this sets up the tell a friend popup in a lightbox as well as sets up the bookmark site link.
 */
$(function()
{
	$('#exhibitor').click(function() {
		$('#cmsformtag').val('subscribe-form-exhibitors');
	});
	
	$('#visitor').click(function() {
		$('#cmsformtag').val('subscribe-form-visitors');
	});
	
	
	// preload images
	$.preLoadImages(
		"../images/pictures/large/system-files/bg_index_page.jpg", 
		"../images/pictures/large/system-files/bg_home_page.jpg",
		"../images/pictures/large/system-files/bg_inner_page.jpg",
		"../images/pictures/large/system-files/bg_product_page.jpg"
	)
	
	
	
	// flyout menu (superfish & supersubs) '
	// i've applyed this menu to the top-menu
	if ($('ul.sf-menu').length) {
		$('ul.sf-menu').supersubs({ 
            	minWidth:    8,   											// minimum width of sub-menus in em units 
            	maxWidth:    27,   											// maximum width of sub-menus in em units 
            	extraWidth:  1  
        	}).superfish({
        		delay:       200,                           					// one second delay on mouseout
        		speed: 		'fast', 						 					// speed up the menu eh :)
            	animation:   {opacity:'show',height:'show'},  	// fade-in and slide-down animation 
				autoArrows: false 											// Hide Arrow for menu items with a dropdown
		});
	}
	
	var def_subs_str, def_search_str, def_prsearch_str;
	
	// attach fancybox to the send to friend link in the footer if both exist
	if ($.fancybox && $('#tellAFriend').length >= 1) {
		$('#tellAFriend').fancybox({
			'autoScale'			: false,
			'scrolling'			: 'no',
			'centerOnScroll'	: true,
			'overlayOpacity'	: 0.5,
			'overlayColor'		: '#000', 
			'showNavArrows'		: false,
			'width'				: 420,
			'height'			: 310,
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'type'				: 'iframe'
		});
	}
	
	// attach jquery bookmark plugin to the bookmark site link.
	if ($('#bookmarkSite').length >= 1) {
		$('#bookmarkSite').jFav();
	}

	// Subscribe box

	def_subs_str=$('#Email_Address').attr('value'); //'enter your email address';

	$('#Email_Address').focus(function()
	{
		if (this.value==def_subs_str)
			this.value='';
	});
	$('#Email_Address').blur(function()
	{
		if (this.value=='')
			this.value=def_subs_str;
	});

	// Products Search box

	//def_prsearch_str=$('#product_search_text').attr('value'); 
	def_prsearch_str='SEARCH DIRECTORY FOR';

	$('#product_search_text').focus(function()
	{
		this.style.color='#707070';
		if (this.value==def_prsearch_str)
			this.value='';
	});
	$('#product_search_text').blur(function()
	{
		this.style.color='#a7a7a7';
		if (this.value=='')
			this.value=def_prsearch_str;
	});

	// Search box

	def_search_str=$('#search_text').attr('value'); //'SEARCH SITE';

	$('#search_text').focus(function()
	{
		this.style.color='#707070';
		if (this.value==def_search_str)
			this.value='';
	});
	$('#search_text').blur(function()
	{
		this.style.color='#a7a7a7';
		if (this.value=='')
			this.value=def_search_str;
	});

	// "Third" nav

	var sel_links=0, all_links=0;
	
	$('#thirdLevelNav a').each(function()
	{
		var tmp_str=''+this.className;
		if (tmp_str.indexOf('-navSelected')>=0)
			sel_links++;
		all_links++;
	});

	if (sel_links>0)
	{
		$('#thirdLevelNav').css('margin-top', '-1px');
		$('#thirdLevelNav').show();
	}
	else
	{
		setTimeout(function()
		{
			if (all_links>0)
			{
				$('#thirdLevelNav').css('margin-top', '-32px');
				$('#thirdLevelNav').show();

				$('#thirdLevelNav').animate(
				{
					'margin-top': '-1'
				}, 400);
			}
		}, 300);
	}


});


/**
 * This is a jquery plugin that I use to preload a bunch of images, its easy to use, just call 
 * 
 * $.preLoadImages("image1.jpg", "image2.jpg")
 * 
 */
(function($) {
	var cache = [];
	// Arguments are image paths relative to the current page.
	$.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);	
		}
	};
})(jQuery);




/* EMAIL FUNCTIONS NEED TO BE PUT INTO AN OVERLAY - REMOVE WHEN DONE */
function email_this_item(i) {
	var win, ht;
	ht=340;
	win=window.open('/pages/email_this_page.php?item='+escape(i), 'email_this_item','resizable,width=420,height='+ht+',left=200,top=120,status');
}


function changePerPage(osel) {

	var val = osel[osel.selectedIndex].value;
	url = new Url(location.href);
	url.setVar('perpage', val);
	url.go();

}

function changeOrder(osel) {
   var val = osel[osel.selectedIndex].value;
   url = new Url(location.href);
   url.setVar('order', val);
   url.go();
}

function toggleDirection(dir) {
   var set = (dir == 'ASC') ? 'DESC' : 'ASC';
   url = new Url(location.href);
   url.setVar('dir', set);
   url.go();
} 

function showHide(element) {
	if(jQuery(element).css('display') == "none"){
		jQuery(element).slideDown('fast');
	}else{
		jQuery(element).slideUp('fast');
	}
}

function over(obj) {
	// first lets see if the src of this image is blank.gif and if its IE6 - Based on these conditions
	// we know that its a png fix so we need to alter the method of changing the image background as oposed
	// to the image source.
	var filename = obj.src.substring(obj.src.lastIndexOf('/')+1);

	// first lets check to see if its IE6 and if the image in a png file
	if(filename == 'blank.gif' && $.browser.msie && $.browser.version == '6.0') {
		var newFilter = (obj.style.filter.replace('.png', '-over.png'));
		obj.style.filter = newFilter;
	} else {
		obj.src=obj.src.replace('.gif', '-over.gif');
		obj.src=obj.src.replace('.jpg', '-over.jpg');
		obj.src=obj.src.replace('.png', '-over.png');
	}
}
 
function out(obj) {
	var filename = obj.src.substring(obj.src.lastIndexOf('/')+1);
	if(filename == 'blank.gif' && $.browser.msie && $.browser.version == '6.0') {
		obj.style.filter = obj.style.filter.replace('-over', '');
	} else {
		obj.src=obj.src.replace('-over', '');
	}
}


function OpenCloseMenuSection(ul_id)
{
	$('#main_left ul li a.store-menu-cat').each(function()
	{
		$(this).removeClass('store-menu-cat-selected');
		$(this).removeClass('downarrow');
	});

	if ($('#main_left #'+ul_id).css('display')=='none')
	{
		var found=0;
		$('#main_left ul.submenu').each(function()
		{
			if (this.style.display!='none')
			{
				$(this).slideUp(150);
				found=1;
			}
		});

		setTimeout(function()
		{
			$('#main_left #'+ul_id).each(function()
			{
				$(this).slideDown(150);
				$(this.parentNode).children('a.store-menu-cat').each(function()
				{
					$(this).addClass('store-menu-cat-selected');
					$(this).addClass('downarrow');
				});
			});
		}, (found?150:1));
	}
	else
	{
		$('#main_left #'+ul_id).slideUp(150);
		$('#main_left #'+ul_id).each(function()
		{
			$(this.parentNode).children('a.store-menu-cat').each(function()
			{
				$(this).addClass('store-menu-cat-selected');
			});
		});
	}
}



