﻿//scripts which are used on whole portal	
function pageInitialize() {
	
	//Handle clear value on unput focus
	handleClearValue();
	
	//Handle megamenu
	handlemegamenu();
	
	// Handle blend
	blend();
	
	//Handle quicktip on room-pointers on homepage and attached products on product details
	$(".quicktip").quicktip();
	
	// Handle costum select box
	costumselectbox();
	
	// Hide containers when they are empty
	jQuery(".massage, .error-massage, #content > span.yes, #content > span.no, #content > span.message_error, .newsletter-info").each(function(e) {
	    var $regioncontent = jQuery(this);
	    if (jQuery.trim($regioncontent.text()).length > 0) {
	        $regioncontent.css("display", "block");
	        $regioncontent.show();
	    }
	});
	
	// Hide containers when they are empty
	$(".news-list-item h4").each(function(e){
		$regioncontent = $(this);
		
		if ($.trim($regioncontent.html()).length < 10) {
			$regioncontent.remove();
		}
	});
}

//scripts which are used only on homepage
function homeInitialize() {	
	//room
	handlemodaldialog();
}

//SCRIPTS PARAMETERS		
		 
// Apply clear value on unput focus
function handleClearValue() {
    $(".clear-value").each(function() {
        var default_value = this.value;
        $(this).focus(function() {
            if(this.value == default_value) {
                this.value = '';
            }
        });
        $(this).blur(function() {
            if(this.value == '') {
                this.value = default_value;
            }
        });
    });
}
	
// Build MegaMenu (product-box slide)
function handlemegamenu(){ 
	
	function addMega(){
	$($(this).find("div.product-box")).slideDown("fast");
	$(this).addClass("hovering");
	}
	
	function removeMega(){
	$($(this).find("div.product-box")).slideUp("fast");
	$(this).removeClass("hovering");
	} 
	
	var megaConfig = {
	interval:30,
	sensitivity:4,
	over:addMega,
	timeout:400,
	out:removeMega
	};
	
	$("li.mega").hoverIntent(megaConfig)

}
	
	
// room: Build modal dialog on homepage
function handlemodaldialog(){ 
	 
	//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var id = $(this).attr('href');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn(2000);

		$(document)
        .bind("keyup", closeRoomModal_escape)
        .bind("keydown", closeRoomModal_escape)
        .bind("keypress", closeRoomModal_escape)
        .bind("keydown", closeRoomModal_escape);
	
	});
	
	//if close button is clicked
	$('.window .close').click(function(e) {
	    closeRoomModal(e);
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});
}

function closeRoomModal_escape(e) {
    if (e.ctrlKey || e.altKey) return;
    if (e.which === 27) {closeRoomModal(e);}
};

function closeRoomModal(e) {
    if(e) e.preventDefault();
    $('#mask').hide();
    $('.window').hide();
}

// Apply blend effect
function blend(){ 
	$(".blend").blend(900);
}

// Apply costum select box
function costumselectbox(){ 
	$("#sort-option select, .ctrlHolder-dropdown select").sb({
	animDuration: 500,
	fixedWidth: true
		});
}

