/* 
Project: Neckermann.hu Frontend
Created by: Zoltan Radics / zoltan.radics@grey.hu
*/

jQuery(function() {
	initBubble();
	initFloatingBar();
	initSlideshow();
	accInfoColumnEqualisation();
	
	jQuery( ".sliderSample" ).slider({ range: true, min: 0, max: 500000, step: 10000, values: [ 50000, 400000 ], slide: function( event, ui ) { jQuery( ".slideSampleAmount" ).val( ui.values[ 0 ] + ' Ft - ' + ui.values[ 1 ] + ' Ft' ); } });
	
	jQuery('.forgottenPassword').click(function() {
		initOverlay();
		return false;
	});
	
	if (jQuery('.rateSummaryDisplay').length > 0) {
		jQuery('.rateSummaryDisplay').mouseenter(function() {
			var rateSummaryDisplayPos = jQuery(this).position();
			var rateSummarySubDisplayOffsetX = ( (jQuery('.rateSummarySubDisplay', this).width() - jQuery(this).width())/2 );
			jQuery('.rateSummarySubDisplay', this).css('left', ( rateSummaryDisplayPos.left - rateSummarySubDisplayOffsetX ) );
			jQuery('.rateSummarySubDisplay', this).css('top', ( rateSummaryDisplayPos.top - (jQuery('.rateSummarySubDisplay', this).height()+5) ) );
			jQuery('.rateSummarySubDisplay', this).fadeIn();
		});
		jQuery('.rateSummaryDisplay').mouseleave(function() {
			jQuery('.rateSummarySubDisplay', this).fadeOut();
		});
	}
	
	// ---------------- LOGO POSITIONING ---
	
	jQuery('#logo').css('left', (jQuery(window).width()/2-460) );
	
	
	// ---------------- CALENDAR IMAGE OFFER ---
	
	jQuery('#imageOffer').cycle({
		fx: 'fade',
		pager: '#imageOfferNav'
	});
			
	// ---------------- ACCOMODATION PAGE TABS AND GRIDS ---
	
	jQuery('.accInfoTabHeader li a').click(function() {
		jQuery('.activeTab').removeClass('activeTab');
		jQuery(this).addClass('activeTab');
		
		tab = jQuery(this).attr('href');
		jQuery('.visible').hide();
		jQuery(tab).slideDown(10,accInfoColumnEqualisation).addClass('visible');
		return false;
	});
	
	jQuery('#accTabImages').cycle({ fx: 'fade', speed: 'fast', timeout: 0, next: '#next2', prev: '#prev2' });
	
	// Grid
	var c = 0;
	jQuery('.accTabHeadGrid > li').each(function() {
		c++;
		if ( !is_int(c/2) ) { jQuery(this).addClass('shaded'); }
	});
	
});

jQuery(document).ready(function () {
	
	function megaHoverOver(){
		jQuery(this).find(".floatingBarPopup").css('top', (-14-jQuery(this).find(".floatingBarPopup").height()));
		jQuery(this).find(".floatingBarPopup").stop().fadeTo('fast', 1).show();
		
	}
	
	function megaHoverOut(){ 
	  jQuery(this).find(".floatingBarPopup").stop().fadeTo('fast', 0, function() {
		  jQuery(this).hide(); 
	  });
	}
 
	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 50, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 200, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};
 
	jQuery("ul.floatingBarContent li .floatingBarPopup").css({'opacity':'0'});
	jQuery("ul.floatingBarContent li").hoverIntent(config);
});


jQuery(window).resize(function() {
	jQuery('#logo').css('left', (jQuery(window).width()/2-460) );
});

function is_int(value) { 
	if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
		return true;
	} else { 
		return false;
	} 
}

function initOverlay() {
	jQuery('#popupLayer').fadeIn();
	jQuery('#popupLayer').css('height', jQuery(document).height());
	jQuery('#closeOverlay, .closeOverlay').click(function() {
		jQuery('#popupLayer').fadeOut('fast');
	});
}

var bubbleContent = new Array();
function initBubble() {
	jQuery('.menuBar li a').mouseover(function() {
		jQuery('#bubble').remove();
		var length = this.id.length;
		currentContent = bubbleContent[this.id.substring(length - 1 , length )];
		if(currentContent == ''){ //the empty content is not shown.
			return false;
		}
		jQuery('body').append(
				'<div id="bubble"><div class="bubbleTop"></div><div class="bubbleMiddle">' + 
				currentContent + '</div><div class="bubbleBottom"></div></div>'
		);
		var menuItemPos = jQuery(this).position();
		var bubbleOffsetX = ( jQuery('#bubble').width() - jQuery(this).width() - 20  );
		jQuery('#bubble').css('left', ( menuItemPos.left - bubbleOffsetX ) );
		jQuery('#bubble').css('top', ( menuItemPos.top - jQuery('#bubble').height() ) );
		jQuery('#bubble').fadeIn('fast');
		return false;
	});
	jQuery('.menuBar li a').mouseout(function() {
		jQuery('#bubble').remove();
	});
	// tooltips
	jQuery('a.addToCompare').attr('title','Az ajánlat összehasonlításhoz adása.');
	jQuery('a.addToFavorites').attr('title','Az ajánlat kedvencekhez adása. A kedvenceket az alsó menüből érheti el.');
}

function initFloatingBar() {
	var cont = '#floatingBar';
	
	var status = getCookie("floatingBarStatus");
	if (status != null && status == 0) {
		jQuery(cont).hide();
		jQuery('#floatingBarClosed').show();
	} else {
		jQuery(cont).show();
	}
	
	jQuery(cont + ' .close').click(function() {
		setCookie("floatingBarStatus", 0, 365);
		jQuery(cont).hide();
		jQuery('#floatingBarClosed').show();
		return false;
	});
	
	jQuery('#floatingBarClosed').click(function() {
		setCookie("floatingBarStatus", 1, 365);
		jQuery(this).hide();
		jQuery(cont).show();
		return false;
	});
}

function initSlideshow() {
	var n = jQuery('#imageset1 li').size();
	var width = jQuery('#imageset1 li').width();
	jQuery('.slideshow').append('<ul id="imageset2" class="images">' + jQuery('#imageset1').html() + '</ul>');
	jQuery('#imageset1').css('width', n * width + 'px');
	jQuery('#imageset2').css('width', n * width + 'px');
	moveSlideshow(n * width);
}

var scrollingSpeed = 35;
function moveSlideshow(imagesetWidth) {
	var slideDuration = imagesetWidth * scrollingSpeed;
	jQuery('#imageset1').css('left', '0px');
	jQuery('#imageset2').css('left', imagesetWidth + 'px');
	jQuery('#imageset1').animate({ left: '-' + imagesetWidth }, { duration: slideDuration, specialEasing: { left: 'linear' } });
	jQuery('#imageset2').animate({ left: '-5' }, { duration: slideDuration, specialEasing: { left: 'linear' }, complete: function() { moveSlideshow(imagesetWidth); } });
}

function accInfoColumnEqualisation() {
	var accInfoWideHeight = jQuery('.accInfoWide .accInfoTabs').height();
	var accInfoNarrowHeight = jQuery('.accInfoNarrow .box').height();
	var maximumHeight = Math.max(accInfoWideHeight, accInfoNarrowHeight) + 20;
	jQuery('.accInfoNarrow').css('height', maximumHeight + 'px');
	jQuery('.accInfoWide').css('height', maximumHeight + 'px');
	
}

function showBlockUI(selector){
	jQuery.blockUI({ 
		message: jQuery(selector),
		css: { position: 'fixed', top: 0, cursor: 'auto', border: 0, width: '100%', left: 'auto' }
	}); 
}

function blockuiPopup(selector) {
	// append ok button to content if it doesn't exist
	if (jQuery(selector).html().indexOf('okButton') == -1) {
		jQuery(selector).append('<br/><input onclick="jQuery.unblockUI();" id="okButton" class="button" value="Ok" type="button">')
	}
	
	jQuery.blockUI({ 
		message: jQuery(selector),
		css: { border: '3px solid #00E8E8' }
	}); 
}

function setCookie(c_name, value, expiredays, path) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) + ";path=" + (path == null ? "/" : path) +
		((expiredays==null || typeof(expiredays) == "undefined") ? "" : ";expires=" + exdate.toUTCString());
}

function getCookie(c_name) {
	if (document.cookie.length > 0) {
	  c_start = document.cookie.indexOf(c_name + "=");
	  if (c_start != -1) {
	    c_start = c_start + c_name.length + 1;
	    c_end = document.cookie.indexOf(";", c_start);
	    if (c_end == -1) {
	    	c_end = document.cookie.length;
	    }
	    return unescape(document.cookie.substring(c_start, c_end));
	  }
	}
	
	return null;
}
