// Image Preloading
jQuery.preloadImages = function(){
	var img_list = ($.isArray(arguments[0]))?arguments[0]:arguments;
	
	for(var i = 0; i<img_list.length; i++){
		jQuery("<img>").attr("src", img_list[i]);
	}
}	

var api_ol, saveLoader;

// prevent console.log from breaking in non-firefox
if (typeof console == "undefined") var console = { log: function() {} };

//try { document.execCommand("BackgroundImageCache", false, true); }catch(e) {}

$(function() {	
	
	if ( jQuery.browser.msie ) {
		$('.hover').hover(
			function() {
				$(this).addClass( 'iehover' );
			},
			function() {
				$(this).removeClass( 'iehover' );
			}
		);
	}	
	
	$('.rollover').hover(
		function() {
			this.src = this.src.replace( '-up', '-over' );
		},
		function() {
			this.src = this.src.replace( '-over', '-up' );
		}
	);	
	
	$('span.mailme').each(function(){
		var at = / at /;
		var dot = / dot /g;
		var addr = $(this).text().replace(at,"@").replace(dot,".");
		$(this).after('<a href="mailto:'+addr+'">'+ addr +'</a>');
		$(this).remove();
	});
	
	// Fix labels with images
	if($.browser.msie){
		$img = $("label img");
		$img.click(function(e){
			$("#" + $(this).parent().attr("for")).change().click();
		});	
	}
		
	
	/* Open Links in Parent Frame */
	$('a.parent').click(function(e){
		e.preventDefault();
		window.top.location = $(this).attr('href');
	});
	
	/* set up goal links */
	$('a.goal').click(function(e){
		e.preventDefault();
		
		var goal = $(this).attr( 'rel' );
		var url = $(this).attr( 'href' );
		
		$.get(
			'http://jazz.closetmaid.com/php/set_goal.php',
			{ goal: goal },
			function() { window.location = url; }
		);
	});
	
	$('a.goal_parent').click(function(e){
		e.preventDefault();
		
		var goal = $(this).attr( 'rel' );
		var url = $(this).attr( 'href' );
		
		$.get(
			'http://jazz.closetmaid.com/php/set_goal.php',
			{ goal: goal },
			function() { window.top.location = url; }
		);
	});
	
	$('a.goal_redirect').each( function() {
		var goal = $(this).attr( 'rel' );
		var url = $(this).attr( 'href' );
		$(this).attr( 'href', 'http://jazz.closetmaid.com/php/set_goal.php?goal=' + goal + '&redirect=' + url );
	});
	
	/* Store */
	$('input.boxradio').each(function(){
		$(this).addClass('hide_offscreen');		
	});
	
	$('label.boxradio:not(.disabled)').each(function(){
		$( this ).bind (
			"click",
			function(){
				var inputField = $('input[id='+$(this).attr('for')+']');
				var fieldName = inputField.attr('name');			
				$('input[name='+fieldName+']:not(:disabled)').each(function(){			
					$('label[for='+$(this).attr('id')+']').removeClass('selected');
				});													 
						
				$(this).addClass('selected');
			}
		);				   
	});
	
	$('input.btn_form_submit').hide();
	$('a.btn_form_submit').show();
	
	/* Configurator Submission */	
	saveLoader = $('div.saveLoader');
	if ( saveLoader.size() > 0 ) {		
		api_ol = saveLoader.overlay ({		
			mask: {
				color: '#FFF',
				opacity: 0
			},
			closeOnClick: false,
			closeOnEsc: false,
			left: '600px',
			top: 'center',
			speed: '0'
		}).data("overlay");	
	
		$("form#form_config input:not(:text)").click(function(e){	
							
			saveLoader.html('<img src="/Style%20Library/Images/loader_bg333.gif" width="16" height="16" alt="">');
		
			try{ 
				api_ol.load(); 
			}catch(err){ 
				saveLoader.css('position','absolute');
				saveLoader.css('top','250px');
				saveLoader.css('right','300px');
				saveLoader.show(); 
			}
		
			$.ajax({
					url: "http://jazz.closetmaid.com/php/add_to_db_redirect.php", 				
					data: "table="+$("input[name='table']").val()+"&"+$(this).attr('name')+"="+$(this).val(),
					success: function(data) {							
						if(data=='failure'){
							saveLoader.html("<p>Error</p>");
							setTimeout(function(){ 
								try{
									api_ol.close(); 
								}catch(err){ 
									saveLoader.hide(); 
								}
							}, 1000);	
						}else{							
							try{
								api_ol.close(); 
							}catch(err){ 
								saveLoader.hide(); 
							}
						}
					},
					error: function() {			
						saveLoader.html("<p>Error</p>");
						setTimeout(function(){ 
							try{
								api_ol.close(); 
							}catch(err){ 
								saveLoader.hide(); 
							}
						}, 1000);	
					},
					timeout: 5000
			});
		});
	}
});

$(window).bind( 'load', function() {
	// start preloading images after page is done
	var imagePaths = new Array();	
	// hover images
	$('.rollover').each( function() {
		s = $(this).attr("src").replace( '-up', '-over' );
		imagePaths.push( s );			
	});
	
	// Main Nav Images
	imagePaths.push("/Style%20Library/Images/mainnav_getideas-over.png");
	imagePaths.push("/Style%20Library/Images/mainnav_design-over.png");
	imagePaths.push("/Style%20Library/Images/mainnav_prepare-over.png");
	imagePaths.push("/Style%20Library/Images/mainnav_shop-over.png");
	imagePaths.push("/Style%20Library/Images/mainnav_install-over.png");
	imagePaths.push("/Style%20Library/Images/mainnav_myclosetmaid-over.png");
	imagePaths.push("/Style%20Library/Images/mainnav_myclosetmaid_sub-over.png");
	// Blue Buttons
	imagePaths.push("/Style%20Library/Images/btn_lg_blue_left-over.png");
	imagePaths.push("/Style%20Library/Images/btn_lg_blue_right-over.png");
	imagePaths.push("/Style%20Library/Images/btn_lg_blue_right_plus-over.png");
	imagePaths.push("/Style%20Library/Images/btn_sm_blue_left-over.png");
	imagePaths.push("/Style%20Library/Images/btn_sm_blue_right-over.png");
	// Gray Buttons
	imagePaths.push("/Style%20Library/Images/btn_sm_gray_left-over.png");
	imagePaths.push("/Style%20Library/Images/btn_sm_gray_right-over.png");
	// Gray Arrows
	imagePaths.push("/Style%20Library/Images/icon_garrow_lg-over.png");
	imagePaths.push("/Style%20Library/Images/icon_garrow_lg_left-over.png");
	imagePaths.push("/Style%20Library/Images/icon_garrow_sm-over.png");
	imagePaths.push("/Style%20Library/Images/icon_dgarrow_sm-over.png");
	// Configuration Menu
	imagePaths.push("/Style%20Library/Images/configmenu_num-over.png");
	imagePaths.push("/Style%20Library/Images/configmenu_content-over.png");
	imagePaths.push("/Style%20Library/Images/loader_bg333.gif");
	// Get Ideas Gallery
	imagePaths.push("/Style%20Library/Images/gallery/btn_save-over.png");
	imagePaths.push("/Style%20Library/Images/gallery/btn_save-working.png");
	imagePaths.push("/Style%20Library/Images/gallery/btn_gallery-over.png");
	imagePaths.push("/Style%20Library/Images/gallery/btn_share-over.png");
	imagePaths.push("/Style%20Library/Images/gallery/btn_zoom-over.png");
	imagePaths.push("/Style%20Library/Images/gallery/icon_garrow_hg_dark_left-over.png");
	imagePaths.push("/Style%20Library/Images/gallery/icon_garrow_hg_dark-over.png");
	imagePaths.push("/Style%20Library/Images/gallery/icon_garrow_lg_dark_left-over.png");
	imagePaths.push("/Style%20Library/Images/gallery/icon_garrow_lg_dark-over.png");
	
	// preload the images
	$.preloadImages(imagePaths);		
}); 

// setup function for custom overlays
function imageOverlay( imagePath ) {
	// make sure previous overlays are hidden
	$('#image_overlay').remove();
	
	// create an overlay div with close button in it
	var overlayDiv = $('<div id="image_overlay" class="popup_content"><a class="close" id="btn_image_close" href="javascript:void(0);"><img alt="Close" src="/Style%20Library/Images/gallery/btn_close.png"></a></div>');
	
	// add the specified image to the div and add it to the page
	var img = $('<img>')
		.attr( 'src', imagePath )
		.appendTo( overlayDiv )
	;
	$('body').append( overlayDiv );
	
	// trigger overlay
	$('#image_overlay').overlay ({
		mask: {
			color: '#000',
			opacity: 0.6
		},
		speed: 'fast',
		load: true
	});
}
