/**
 * Initialize $.CORE in case it's not present
 */
if(typeof $.CORE == "undefined") {
	$.CORE= Object;
};

/**
 * Dummy object
 */
$.CORE.dummyobject = {
	construct : function() {
	},
	
	dummyFunction : function() {
	}
};

/**
 * Extra classes
 */
$.CORE.extraclasses = {
	construct : function() {
		this.execExtraClasses();
	},
	
	execExtraClasses : function() {
		$('.productbox:odd').addClass('productbox_odd');
		$('.producttable td:last-child, .producttable th:last-child, .reference_item:last-child').addClass('last');
	}
};


/**
 * Forms
 */
$.CORE.forms = {
	construct : function() {
		this.execFormCheckerPlugin();
	},
	
	execFormCheckerPlugin : function() {
		var $formRef = $('form.formGenerated');
		if($formRef.length) {
        	$formRef.formChecker();
		}
	}
};


/**
 * Forms
 */
$.CORE.clearfields = {
	construct : function() {
		this.execClearFields();
	},
	
	execClearFields : function() {
		$('.clearfield').clearField();
	}
};

/**
 * Superfish
 */
$.CORE.superfish = {
	construct : function() {
		this.execSuperfish();
	},
	
	execSuperfish : function() {
		$('#mainmenu').superfish({
			dropShadows: false				 
		});
	}
};

/**
 * Cycle banner
 */
$.CORE.banner = {
	construct : function() {
		this.execBanner();
	},
	
	execBanner : function() {
		
		$('#slidecontainer').cycle({
				fx:'fade',
				speed:500, 
				timeout:5000,
				startingSlide: 0,
				pager:'#banner_pager',
				pagerAnchorBuilder: function(idx, slide) {
					var url = $('.banner_info_link',slide).attr('href');
					var pagertitle = $('.pagertext span.title',slide).text();
					var pagersubtitle = $('.pagertext span.subtitle',slide).text();
					return '<a id="pager'+idx+'" href="'+url+'"><span class="title">'+pagertitle+'</span><span class="subtitle">'+pagersubtitle+'</span></a>';
				}
		});
		
		$('#banner_pager a').bind({
		   	click: function(event) {
				if(event.originalEvent){ 
					window.location=$(this).attr('href');
				}else{
					event.preventDefault();
				};
			},
			mousemove: function(event) {
				$(this).click();
		  	}
			
		});

	}
};


/**
 * Cycle spotlight
 */
$.CORE.spotlight = {
	construct : function() {
		this.execSpotlight();
	},
	
	execSpotlight : function() {
		$('#spotlight_slidebox').cycle({
				fx:'scrollHorz',
				speed:1000, 
				timeout:5000,
				pause:1,
				pauseOnPagerHover: 1,
				startingSlide: 0,
				pager:'#spotlight_pager'
		});
		
		$('#spotlight_pager a').click(function(event) {
  			event.preventDefault();
		});
		
	}
};

/**
 * Cycle references
 */
$.CORE.references = {
	construct : function() {
		this.execReferences();
	},
	
	execReferences : function() {
		$('#reference_slidebox').cycle({
				fx:'scrollHorz',
				speed:1000, 
				timeout:5000,
				pause:1,
				pauseOnPagerHover: 1,
				startingSlide: 0,
				pager:'#reference_pager'
		}).cycle('pause');
		
		$('#reference_pager a').click(function(event) {
  			event.preventDefault();
		});
		
	}
};


/**
 * Login slide
 */
$.CORE.loginslide = {
	construct : function() {
		this.execLoginSlide();
	},
	
	execLoginSlide : function() {
		$('.site_wrapper').css('top','-60px').addClass('dealerLoginClosed');
	
		$('#dealer_btn').click(function(event){
			if ($('.dealerLoginClosed').length != 0) { 
				$('.site_wrapper').animate({top: '0px'},500).addClass('dealerLoginOpen').removeClass('dealerLoginClosed');	
			} else {
				$('.site_wrapper').animate({top: '-60px'},500).addClass('dealerLoginClosed').removeClass('dealerLoginOpen');	
			}
		});
		
	}
};

/**
 * Productmenu
 */
$.CORE.productmenu = {
	construct : function() {
		this.execProductMenu();
	},
	
	execProductMenu : function() {
		$('.closed .productmenu_list').hide();
		$('.productmenu_title').click(function(event){
			event.preventDefault();									   
			if ($(this).parents('.closed').length != 0) { 
				$('.productmenu_category').each(function(){
					$(this).removeClass('open').addClass('closed').children('.productmenu_list').slideUp(500);
				})
				$(this).parents('.productmenu_category').removeClass('closed').addClass('open').children('.productmenu_list').slideDown(500);
			} else {
				$(this).parents('.productmenu_category').removeClass('open').addClass('closed').children('.productmenu_list').slideUp(500);
			}
		});
		
		$('.productmenu_list li').click(function(event){
			$(this).siblings().removeClass('current');
			$(this).parents().removeClass('current');
			$(this).addClass('current');
		});
		
		$('.productmenu_list li a[href="#"]').click(function(event){
			event.preventDefault();	
		});
		
	}
};

/**
 * Searchbutton
 */
$.CORE.searchslide = {
	construct : function() {
		this.execSearchSlide();
	},
	
	execSearchSlide : function() {
		$('.productview #search_box form').hide();
		$('.productview #search_box').addClass('closed');
		$('#search_title').click(function(event){
			event.preventDefault();									   
			if ($(this).parents('.closed').length != 0) { 
				$('#search_box').removeClass('closed').addClass('open');
				$('#search_box form').slideDown(500);
			} else {
				$('#search_box').removeClass('open').addClass('closed');
				$('#search_box form').slideUp(500);
			}
		});
		
	}
	
};

/**
 * Fancybox
 */
$.CORE.fancybox = {
	construct : function() {
		this.execFancyBox();
	},
	
	execFancyBox : function() {
		$("a[rel='lightbox']").fancybox({
			overlayOpacity: 0.8
		}); 
		
	}
	
};


/**
 * Executes when the DOM has been fully loaded
 */
$(document).ready(function() {
	$.CORE.extraclasses.construct();
	$.CORE.superfish.construct();
	$.CORE.forms.construct();
	$.CORE.clearfields.construct();
	$.CORE.banner.construct();
	$.CORE.spotlight.construct();
	$.CORE.references.construct();
	$.CORE.loginslide.construct();
	$.CORE.productmenu.construct();
	$.CORE.searchslide.construct();
	$.CORE.fancybox.construct();
});
