// Prevent console commands from throwing errors in IE
try { console.log('init console... done'); } catch(e) { console = { log: function() {} }; }
// Fix JS flash of unstyled content by hiding all descendents of HTML until DOM is loaded.


function initColorbox(){
	$('div.entry a').each(function(){
		if(
			( $(this).attr('href').indexOf('.jpg') > -1 ) ||
			( $(this).attr('href').indexOf('.jpeg') > -1 ) ||
			( $(this).attr('href').indexOf('.gif') > -1 ) ||
			( $(this).attr('href').indexOf('.png') > -1 )
		)
			$(this).colorbox({scalePhotos:"true", maxWidth:"90%", maxHeight:"90%"});
	});

	$('a.colorbox').colorbox({transition:"elastic", width:"760px", height:"500px"});
	$('#ibsPhotoWidget a.widgetGridThumb').colorbox({transition:"elastic", width:"760px", height:"500px", iframe: true });
	$('a.PDFpreview').colorbox({transition:"elastic", width:"600px", height:"700px"});

}


var merillat = {
	setGlobals: function() {
		$mainWrapper = jQuery('#main-wrapper');
		$contentWrapper = jQuery('#content-wrapper');
		$header = jQuery('#header');
		$nav = jQuery('#main-nav');
		$navPoints = jQuery('#main-nav > li > a');
		$signInForm = jQuery('#sign-in-form');
		$myDealers = jQuery('div#my-dealers-container');
		$manageProjectLink = jQuery('a#manage-project');
		$addProjectLink = jQuery('a#add-project');
		$styleDescriptionLink = jQuery('a#style-description');
		$manageDesignLink = jQuery('a#manage-design');
		$curURL = document.URL;
	},

	// this can be loaded after critical content
	setupSubNav: function() {
		$navPoints.each(function() {
			var $this = jQuery(this);
			var matchId = '#subnav-' + $this.attr('id');
			$this.after($(matchId));
			//insert subnav list into li
			//$this.after(jQuery(data).filter(matchId));
			
			
			//hide subnavs initially
			var $sib = $this.siblings('.subnav');
			$sib.find('#kitchen-planner-link').click(function() {

				var newwindow = window.open(jQuery(this).attr('href'), 'KitchenPlannerWindow');
				newwindow.focus();

				return false;
			});
			jQuery('ul.subnav').hide();
			Cufon.replace('ul.subnav a', { hover: true, textShadow: '-1px -1px rgba(255, 255, 255, 0.4), 1px -1px rgba(255, 255, 255, 0.4), -1px 1px rgba(255, 255, 255, 0.4), 1px 1px rgba(255, 255, 255, 0.4)' });

			$this.parent().hoverIntent(function() {
				$sib.slideDown();
			}, function() {
				$sib.slideUp();
			});
		});

	},

	viewSelector: function() {
		jQuery('.view-link-container').click(function() {
			jQuery('#list-option-container').find('.selected').removeClass('selected');
			jQuery(this).addClass('selected');
			jQuery('ul.wood-finishes li span').fadeOut('slow', function() {
				//jQuery(this).fadeIn('fast').addClass('thumb_view');
			});
			return false;
		});
	},

	setButtonStyles: function() {
		var $myWidth, $myHeight;
		var $myHorzPadding = 0;
		var $myVertPadding = 0;

		var $myHtml, $curButtonLink, $curButton;
		var i = 0;
		jQuery('button.button-replacer').each(function() {
			$myHtml = jQuery(this).html();
			jQuery(this).css({ 'position': 'absolute', 'width': '20px', 'height': '10px', 'top': '0', 'left': '0' });
			jQuery(this).wrap('<div style="position: relative; display: inline;"></div>');
			if (jQuery(this).hasClass('arrow')) {
				jQuery(this).after('<a href="#" class="button-replaced button-replacer arrow">' + $myHtml + '</a>');
			} else {
				jQuery(this).after('<a href="#" class="button-replaced button-replacer">' + $myHtml + '</a>');
			}
		});

		jQuery('button.button-replacer-primary').each(function() {
			$myHtml = jQuery(this).html();
			jQuery(this).css({ 'position': 'absolute', 'width': '20px', 'height': '10px', 'top': '0', 'left': '0' });
			jQuery(this).wrap('<div style="position: relative; display: inline;"></div>');
			if (jQuery(this).hasClass('arrow')) {
				jQuery(this).after('<a href="#" class="button-replaced button-replacer-primary arrow">' + $myHtml + '</a>');
			} else {
				jQuery(this).after('<a href="#" class="button-replaced button-replacer-primary">' + $myHtml + '</a>');
			}
		});

		jQuery('a.button-replaced').each(function() {
			$curButtonLink = jQuery(this);
			$curButtonLink.click(function() {
				jQuery(this).parents('form').submit();
				return false;
			});
			i = i + 1;
		});

		jQuery('a.button-replacer').each(function() {
			if (jQuery(this).hasClass('arrow')) {
				jQuery(this).append('<span class="button-arrow"></span>');
				jQuery(this).css('padding-right', '14px');
			};
		});

		jQuery('a.button-replacer-primary').each(function() {
			if (jQuery(this).hasClass('arrow')) {
				jQuery(this).append('<span class="button-arrow"></span>');
				jQuery(this).css('padding-right', '14px');
			};
		});



	},

	activateCufon: function() {
		var $buttonReplacers = jQuery('a.button-replacer').not('.project-dropdown a.button-replacer'), $h2s = jQuery('h2').not('.colorbox-inline-content h2');
		Cufon.replace('h1', { fontFamily: 'Helvetica Neue' });
		Cufon.replace('h3.inline-heading', { fontFamily: 'Helvetica Neue' });
		//Cufon.replace('ul#footer-nav a', { hover: true, fontFamily: 'Helvetica Neue' });
		Cufon.replace('#footer-site-links-title', { fontFamily: 'Helvetica Neue' });
		Cufon.replace('h3.your-style strong', { textShadow: '2px 2px rgba(0,0,0,0.1), 1px 1px rgba(0,0,0,0.2)', fontFamily: 'Helvetica Neue' });
		Cufon.replace($buttonReplacers, { fontFamily: 'Helvetica Neue', textShadow: '1px 1px rgba(0, 0, 0, 0.3), 0 1px rgba(0, 0, 0, 0.3), 1px 0 rgba(0, 0, 0, 0.3)' });
		Cufon.replace('a.button-replacer-primary', { fontFamily: 'Helvetica Neue', textShadow: '1px 1px rgba(0, 0, 0, 0.3), 0 1px rgba(0, 0, 0, 0.3), 1px 0 rgba(0, 0, 0, 0.3)' });
    },


	fixFooterBackgroundIE6: function() {
		if (jQuery('#footer-site-links').length > 0) {
			jQuery('#footer-site-links').ifixpng();
		}
	},
	
	setPageUnload: function() {
		$(window).unload(function() {
			$('embed, object').remove();
		});
	}
};

jQuery(document).ready(function() {
	initColorbox();
    merillat.setGlobals();
    lib.externalLinks();
    lib.addFormClasses();
    lib.inputDefault();
	lib.fileLinks();
    merillat.setupSubNav();
    merillat.viewSelector();
    merillat.setButtonStyles();
	merillat.activateCufon();
	merillat.fixFooterBackgroundIE6();
	merillat.setPageUnload();
});