/*
	normont.com (Global Script)
	Copyright 2010 Chauncey McAskill
*/

// Keep track of initiated variables
var $$, vars;

(function () {

	var Site = {
		/**
		* Let's set everything up
		*/
		init : function () {
			$$ = {
				overlay : $('#overlay, a.close-modal'),

				launchLogin : $('#login a.login-link'),
				overlayLogin : $('#overlay, #popupLogin'),
				popupLogin : $('#popupLogin'),

				joinNewsletter : $('#joinNewsletter'),
				overlayNewsletter : $('#overlay, #popupNewsletter'),
				popupNewsletter : $('#popupNewsletter'),

				joinFlyer : $('#joinFlyer'),
				overlayFlyer : $('#overlay, #popupFlyer'),
				popupFlyer : $('#popupFlyer'),
				formPopup : $('#formPopup'),

				overlayDirections : $('#overlay, #popupDirections'),
				popupDirections : $('#popupDirections'),

				contactLink : $('.contactLink'),
				overlayContact : $('#overlay, form#form_info'),
				popupContact : $('form#form_info'),

				quoteLink : $('.quoteLink'),
				overlayQuote : $('#overlay, form#form_quote'),
				popupQuote : $('form#form_quote'),
				
				overlayFormPopup : $('#overlay, #formPopup #iframe_wrapper'),

				contactProduct : $('#field_product'),
				quoteProduct : $('#field_product2'),

				productsMenu : $('li.list-products','ul#navMain'),
				productsSubmenu : $('#productSubmenu'),

				locationMenu : $('.location_menu'),
				locationMenuLocations : $('.location_menu > li > ul'),
				locationMenuButtons : $('.location_menu > li'),

				quickMenu : $('.quick-menu'),
				quickMenuSubs : $('ul.quick-sub','.quick-menu'),
				quickMenuButtons : $('.collapsable','.quick-menu'),

				catalogSub : $('#catalogSub'),
				catalogSubHead : $('#catalogSub .show_products strong'),
				catalogSubTable : $('#catalogSub table.data'),

				contactFormsPanel : $('.contact_forms_panel'),
				contactFormPanels : $('.contact_forms_panel > div'),
				contactFormButtons : $('.contact_forms_panel > a'),

				search : $('#searchform #s')

			}
			vars = {
				onProductsSubmenu : false
			}

			$$.productsSubmenu.appendTo($$.productsMenu);
		}
	};

	$(document).ready(function(){
		Site.init();

		/*$$.joinNewsletter.click(function(){
			$$.overlayNewsletter.show();
			return false;
		});
		$$.joinFlyer.click(function(){
			$$.overlayFlyer.show();
			return false;
		});*/
		$$.launchLogin.click(function(){
			$$.overlayLogin.show();
			return false;
		});
		$$.contactLink.click(function(){
			var IDs = new Array();
			var i = 0;
			$(this).parents('tr').children('td.productID').each(function(){
				IDs[i] = $(this).text();
				i++;
			});
			$$.contactProduct.val( IDs.join(', ') );
			$$.overlayContact.show();
			return false;
		});
		$$.quoteLink.click(function(){
	        var url = $(this).attr('href');
		    
		    if($('iframe#quote_form_iframe').length == 0)
		        $$.catalogSub.append('<div id="iframe_wrapper"><a class="close-modal dynamic" href="#">Close</a><iframe id="quote_form_iframe" frameborder="0"></iframe></div>');
            
            $('iframe#quote_form_iframe').attr('src', url);

            $$.overlayQuote = $('#overlay, #catalogSub #iframe_wrapper');
            $('#overlay').show();
            
            $('iframe#quote_form_iframe').load(function() {
                
                $('.close-modal.dynamic').click(function(){
                    $$.overlayQuote.hide();
                });
            
                $('#catalogSub #iframe_wrapper').show();
            });		
			return false;
		});
		
		$$.openFormPopup = function(url){
		    if(typeof url === 'undefined' )
		        return;        
		    
		    if($('iframe#formPopup_iframe').length == 0)
		        $$.formPopup.append('<div id="iframe_wrapper"><a class="close-modal dynamic" href="#">Close</a><iframe id="formPopup_iframe" frameborder="0"></iframe></div>');
		    /*else
		        //clear the iframe
		        $('iframe#formPopup_iframe').attr('src', 'about:blank');*/
		        
		    $('iframe#formPopup_iframe').attr('src', url);
		    
		    $$.overlayFormPopup = $('#overlay, #formPopup #iframe_wrapper');
            $('#overlay').show();
            
            $('iframe#formPopup_iframe').load(function() {
                var e = document.createElement('link'); 
                e.rel = 'stylesheet'; 
                e.type = 'text/css'; 
                e.href = '/wp-content/themes/normont/style.css' ; 

                $('iframe#formPopup_iframe')[0].contentWindow.document.getElementsByTagName('head')[0].appendChild(e);
                $('iframe#formPopup_iframe')[0].contentWindow.document.getElementsByTagName('html')[0].style.background = 'transparent';
                $('iframe#formPopup_iframe')[0].contentWindow.document.getElementsByTagName('html')[0].style.overflowY = 'auto';

                $('.close-modal.dynamic').click(function(){
                    $$.overlayFormPopup.hide();
                });
                
                $('#formPopup #iframe_wrapper').show();
            });
            		
			return false;
		    
		}
		
		//console.log($$.overlay);
		
		$$.overlay.click(function(){
			$$.overlayFlyer.hide();
			$$.overlayNewsletter.hide();
			$$.overlayDirections.hide();
			$$.overlayLogin.hide();
			$$.overlayContact.hide();
			$$.overlayQuote.hide();
			$$.overlayFormPopup.hide();
			return false;
		});

		$$.locationMenuLocations.hide();
		$$.locationMenuButtons.click(function(){
			var $t = $(this).children('strong');
			$t.parent().toggleClass('open');
			
			$t.next().slideToggle(100);
		});

		$('.address-direction').each(function(){
			$(this).click(function(){
				$(this).next().next().slideToggle();
			})

		});

		$('.general_request_form').show();
		$('.general_request_button').css('font-weight','bold');

		$('.cadmodel_request_form').show();
                $('.cadmodel_request_button').css('font-weight','bold');
		
		$('.general_request_button').click(function(){
			$$.contactFormPanels.each(function(){
				$(this).hide();
			});
			$$.contactFormButtons.each(function(){
				$(this).css('font-weight','normal');
			});
			$(this).css('font-weight','bold');
			$('.general_request_form').show();
		});

		$('.cadmodel_request_button').click(function(){
			$$.contactFormPanels.each(function(){
				$(this).hide();
			});
			$$.contactFormButtons.each(function(){
				$(this).css('font-weight','normal');
			});
			$(this).css('font-weight','bold');
			$('.cadmodel_request_form').show();
		});

		$('.quote_request_button').click(function(){
			$$.contactFormPanels.each(function(){
				$(this).hide();
			});
			$$.contactFormButtons.each(function(){
				$(this).css('font-weight','normal');
			});
			$(this).css('font-weight','bold');
			$('.quote_request_form').show();
		});

		$('.litterature_request_button').click(function(){
			$$.contactFormPanels.each(function(){
				$(this).hide();
			});
			$$.contactFormButtons.each(function(){
				$(this).css('font-weight','normal');
			});
			$(this).css('font-weight','bold');
			$('.litterature_request_form').show();
		});


		$('ul.quick-sub','.quick-menu li:not(.current)').hide();
		$$.quickMenuButtons.click(function(){
			var $t = $(this);
			var $n = $t.next();
			if ($n.is('.quick-sub')) $n.slideToggle(100); else $n.next('.quick-sub').slideToggle(100);
			$t.toggleClass('open');
		});

		if ( $$.catalogSubHead.length > 1 ) $$.catalogSubTable.hide();
		$$.catalogSubHead.click(function(){
			var $t = $(this),
			$n = $t.parent().next();
			$t.parent().toggleClass('open');
			if ($n.is('table.data')) $n.slideToggle(100); else $n.next('table.data').slideToggle(100);
		});

		$$.search.focus(function() {
			var $t = $(this);
			if ($t.val() == $t[0].title) {
				$t.removeClass('placeholder');
				$t.val("");
			}
		}).blur(function() {
			var $t = $(this);
			if ($t.val() == "") {
				$t.addClass('placeholder');
				$t.val($t[0].title);
			}
		}).blur();

		$('a.lightbox').fancybox();

		/*$$.productsMenu.bind('mouseenter', function() {
			position = $$.productsMenu.offset();
			$$.productsSubmenu.delay(100).show();
		}).bind('mouseleave', function() {
			if ( !vars.onProductsSubmenu )
				$$.productsSubmenu.fadeTo(100, 1, function(){
					if ( vars.onProductsSubmenu ) return;
					else $$.productsSubmenu.hide();
				});
		});

		$$.productsSubmenu.bind('mouseenter', function() {
			vars.onProductsSubmenu = true;
			$$.productsSubmenu.show();
		}).bind('mouseleave', function() {
			vars.onProductsSubmenu = false;
			$$.productsSubmenu.hide();
		});*/

	});

	$(window).load(function(){
    
        // preload
        $('<img />').attr('src', '/wp-content/themes/normont/assets/images/navMain.menu-link.hover.png');
/*
		$('a[rel="external"]').attr('target','_blank');
		$('a[href^="http://"]').attr('target','_blank');
*/
	});

})();

