/**
 * Template for a js project.
 * @author NOSE
 * 
 * @version 1.0.0 initial version
 *
 */
var Globus = {
	
	/**
	* Flags.
	*/
	mobile: true,
	
	/**
	 * Initialize.
	 */
	initialize : function(){
		
		// global
		Globus.initFlags();
		Globus.initTemplates();
		
		// template: brand browseload
		if (jQuery("body.brandBrowseload").size() > 0) {
			Globus.initTemplateBrandBrowseload();
		}		
		// template: brand browser
		if (jQuery("body.brandBrowser").size() > 0) {
			Globus.initTemplateBrandBrowser();
		}		
		// template overview
		if (jQuery("body.overview").size() > 0) {
			Globus.initTemplateOverview();
		}		
		// template page
		if (jQuery("body.page").size() > 0) {
			Globus.initTemplatePage();
		}	
		// template modal
		if (jQuery("body.modal").size() > 0) {
			Globus.initTemplateModal();
		}

	},
	
	/**
	* Initializes the flags
	*/
	initFlags: function() {
		
		// by parameter
		var paramMobile = false;
		if (jQuery(document).getUrlParam("mobile")) {
			paramMobile = true;
			
			// set cookie
			var v = jQuery(document).getUrlParam("mobile");
			jQuery.cookie("mobile",v,{expires:1});
		}
		
		// to mobile or not to mobile
		if (paramMobile || jQuery.cookie('mobile') != null) {
			
			// flag & tag
			Globus.mobile = true;
			jQuery("body").addClass("mobile");
			
			// classic
			if (jQuery.cookie('mobile') == "mobile_classic") {
				jQuery("body").addClass("mobile_classic");
			}
			
			// classic
			if (jQuery.cookie('mobile') == "mobile_standard") {
				jQuery("body").addClass("mobile_standard");
			}
		}
		
	},

	/**
	* Initializes global stuff.
	*/
	initTemplates: function() {
		
		// print
		Globus.initLinks();
		
		// dropdown
		Globus.initDropdown();
		
		// selector
		Globus.initSelector();
		
		// store locator
		Globus.initStoreLocator();
		
		// hoverable
		Globus.initHoverable();
		
		// hoverable
		Globus.initLinkable();
		
		// thickbox
		Globus.initThickbox();
		
		// loader
		Globus.initLoader();

		// stepper
		Globus.initStepper();
		
		// sum amount
		Globus.initCalculator();
	},
	/**
	 * Init template page.
	 */
	initTemplatePage : function(){	
		
		// toggler
		if (jQuery("#sideBarRight .toggle").size() > 0) {
			Globus.initTogglerSidebar();
		}
		if (jQuery("#content .toggle").size() > 0) {
			Globus.initTogglerContent();
		}
		
		// gallery (carousel)
		if (jQuery("#gallery").size() > 0) {
			Globus.initGallery();
		}
		
		// tabs
		if (jQuery("div.tabbed").size() > 0) {
			Globus.initTabs();
		}

		// enabler
		if (jQuery(".enabler").size() > 0) {
			Globus.initEnabler();
		}
	},
	/**
	 * Init template page.
	 */
	initTemplateOverview : function(){
		// tabs
		if (jQuery("div.tabbed").size() > 0) {
			Globus.initTabs();
		}
	},
	/**
	* Init template brand browseload.
	*/
	initTemplateBrandBrowseload: function() {
		
	},
	/**
	* Init template brand browseload.
	*/
	initTemplateBrandBrowser: function() {		
		// stations
		jQuery("#stations").itemToggler({selectorToggler:"h3.toggler",itemMessage:false,accordion:true,catchEvent:"mouseover"});
	},
	/**
	* Init template modal.
	*/
	initTemplateModal: function() {
		
		// close modal
		jQuery("#modalClose, .modalClose").click(function(){self.parent.tb_remove();});
		
		// init
		Globus.initTemplatePage();
	},

	/**
	* Initializes the dropdown.
	*/
	initDropdown: function() {
		
		// dropdown
		jQuery(".dropdown").each(function(i,dropdown){
			
			// superfish
			jQuery(dropdown).superfish({animation : { opacity:"show" }});
			
			// bgiframe
			if (jQuery.browser.msie && jQuery.browser.version <= 7) {
				jQuery(dropdown).find(">li:has(ul)")
					.mouseover(function(){
						jQuery("ul", this).bgIframe({opacity:false});
					})
					.find("a")
					.focus(function(){
						jQuery("ul", jQuery(".nav>li:has(ul)")).bgIframe({opacity:false});
					});
			}
			
		});
	},
	/**
	* Initializes the loader.
	*/
	initLoader: function() {
		// loader
		jQuery(".loadit").each(function(i,el){
			
			// add loader
			jQuery(el).addClass("loader");
			
			// events
			jQuery(el).bind("click",function(){
				jQuery(this).removeClass("loadit");		
				return true;
			});
			
		});

	},
	
	/**
	* Initializes the toggler.
	*/
	initTogglerSidebar: function() {
		// item toggler
		jQuery("#sideBarRight").itemToggler({selectorToggler: "h4.toggler", accordion:true});
	},
	
	/**
	* Initializes the toggler.
	*/
	initTogglerContent: function() {
		// item toggler
		var ex = ";"
		var paramExpanded = jQuery(document).getUrlParam("expanded");
		if (paramExpanded) {
			// input selected (for form)
			jQuery("input#"+paramExpanded).attr("checked",true);
			var toggleItems = jQuery("#content .toggle .toggler");
			for (var i = 0; i < toggleItems.length; i++) {
				var cId = jQuery(toggleItems[i]).attr("id");
				if (cId == paramExpanded) {
					ex = i + ";";
					break;
				}
			}
		}
		jQuery("#content .toggle").itemToggler({selectorToggler: ".toggler", selectorItem: ".item", accordion:true, expanded:ex});
	},
	
	/**
	* Initializes the toggler.
	*/
	initEnabler: function() {
		jQuery(".enabler").bind("click",function(){
			var id = jQuery(this).attr("id");
			if (jQuery(this).attr("checked")) {
				jQuery("."+id).slideUp("fast");
			}
			else {
				jQuery("."+id).slideDown("slow");
			}
		});
	},
	
	/**
	* Initializes the tabs.
	*/
	initTabs: function() {
		// tabs
		jQuery('div.tabbed').tabs({selectedClass:"active",hideClass:"hidden"});
	},
	
	/**
	* Initializes the thickbox.
	*/
	initThickbox:function() {	

		// thickbox
		tb_init('a.thickbox, area.thickbox, input.thickbox, a.galleryBlowup');
	},
	
	/**
	* Initializes the stepper.
	*/
	initStepper: function() {
		
		// step by step
		jQuery(".stepper").each(function(i,el){
			jQuery(el).stepper();
		});
        jQuery(".cartStepper").each(function(i,el){
                        jQuery(el).cartStepper();
         });
	},
	
	/**
	* Initialize the hoverable.
	*/
	initHoverable: function() {
			
		// elements
		jQuery(".teaser, .teaserPlain, .teaserImage, .teaserImageLeft, #sideBarRight .box, .buttonBox").each(function(i,hoverable){
			
			// link
			var h = null;
			
			// hoverable
			if (jQuery("ul.buttons li a",hoverable).length > 0) {
				
				// tag
				jQuery(hoverable).addClass("hoverable");
				
				// prepare
				var btns = jQuery("ul.buttons li a",hoverable);
				var btnlink = btns[btns.size()-1];
				
				// href
				h = jQuery(btnlink).attr("href");

				// events
				jQuery("img",hoverable).bind("mouseenter",imageEnter);
				jQuery("img",hoverable).bind("mouseleave",imageLeave);
				jQuery("img, h2, h3",hoverable).bind("click",doLink);
					
			}
			// no buttons
			else {
				jQuery(hoverable).addClass("nobuttons");
			}
			
			/*
			* Functions.
			*/
			function imageEnter() {
				// do stuff
				jQuery(this).animate({opacity: 0.93}, 180);
			}
			function imageLeave() {
				// do stuff
				jQuery(this).animate({opacity: 1.0}, 300);
			}
			function doLink() {
				// follow link
				window.location.href = h;
			}
			
		});
		
		
	},
	
	/**
	* Initialize the linkable.
	*/
	initLinkable: function() {
		
		// elements
		jQuery("div.news, div.linkable").each(function(i,news){
			
			// link
			var h = null;
			
			// hoverable
			if (jQuery("a",news).length > 0) {
				
				// prepare
				var btns = jQuery("a",news);
				var btnlink = btns[0];
				
				// href
				h = jQuery(btnlink).attr("href");

				// events
				jQuery(news).bind("click",function(){
					if (h != null) {
						window.location.href = h;
					}
				});
				
				// cursor
				jQuery(news).css({"cursor":"pointer"});
					
			}

		});
		
	},
	
	/**
	* Initialize the selector.
	*/
	initSelector: function() {
		
		// selectors
		jQuery(".selector").each(function(i,sel){
			jQuery(sel).selector();
		});
		
	},
	
	
	/**
	* Initialize the store locator.
	*/
	initStoreLocator: function() {
		
		// selectors
		jQuery(".locations").each(function(i,loc){
			jQuery(loc).storeLocator();
		});
		
	},
	
	/**
	* Initializes the gallery.
	*/
	initGallery: function() {
		
		
		// slides
		jQuery("#gallery").each(function(i,g){
			
			// init
			jQuery(g).slides();
			
		});
	},
	
	/**
	* Initializes the calculator.
	*/
	initCalculator: function() {
		
		// bind
		jQuery("input.changeAmount").each(function(i,ca){
			jQuery(ca).bind("change", function (){Globus.sumAmount(this)});
		});
		
		/**
		* Sum Ammount.
		*/
		function sumAmount(e) {

			// values
			valueOne = parseInt(jQuery("#formAmount10").val());
			valueTwo = parseInt(jQuery("#formAmount20").val());
			valueThree = parseInt(jQuery("#formAmount50").val());

			// check
			if (isNaN(valueOne)) valueOne = 0;
			if (isNaN(valueTwo)) valueTwo = 0;
			if (isNaN(valueThree)) valueThree = 0;

			// sum
			varSum = (valueOne*10) + (valueTwo*20) + (valueThree*50);
			jQuery("#formTotalAmount").val(varSum);
		}
	},
	
	/**
	* Initializes the links.
	*/
	initLinks: function() {
		
		// print links
		jQuery("a.printLink").bind("click",function(){window.print();return false;});
	}
	
}
jQuery(document).ready(function(){
	Globus.initialize();
});




