
// A CreativeScript by Lukasz Tyrala (creativestyle.de) <l.tyrala at gmail accout>
// This uses jQuery (http://jquery.com/) library

function scale_thumbs() {
	// SCALING PRODUCT THUMBS
	// ----------------------------------------------------------------------------

	// If image exceeds maximal width/height one will be scaled according to its longer dimetion
	// This applies to all <img /> elements with class set to "thumb-product"

		$("img.thumb-product").each(function () {

			var img_width_max = 150; // Insert requested desired maximum _width_ for thumbs here
			var img_height_max = 150; // Insert requested desired maximum _height_ for thumbs here

			var img_width = $(this).width();
			var img_height = $(this).height();

			if (img_width > img_height && img_width > img_width_max) { $(this).width(img_width_max); }
				else { if (img_height>img_height_max) { $(this).height(img_height_max); } }

		});

	// End of SCALING PRODUCT THUMBS
}

/*********** Hover effect for best price buttons on home page  ****************/
this.bestPriceToolTip = function() {
	xOffsetBp = 10;
	yOffsetBp = 20;

	$(".bpv img").hover(function(e){
			$("body").append('<div id="tooltip"><div id="title">Preisvergleich?</div>'+
							 'Der Preis dieses Angebots wurde mit Hilfe eines Online Preisvergleichs getestet. Das Ergebnis entspricht dem besten Preis den wir bei unserer Recherche finden konnten. So könnt Ihr Euch ganz sicher sein auch wirklich ein Schnäppchen zu machen.'+
							 '</div>');
			$("#tooltip")
				.css("top", (e.pageY - xOffsetBp) + "px")
				.css("left", (e.pageX + yOffsetBp) + "px")
				.fadeIn("fast");
		},
		function(e) {
			$("#tooltip").remove();
		}
	)
	$(".bpv").mousemove(function(e){
		$("#tooltip")
			.css("top", (e.pageY - xOffsetBp) + "px")
			.css("left", (e.pageX + yOffsetBp) + "px");
	});
}

$(document).ready(function(){
	scale_thumbs();
	bestPriceToolTip();

	if ($.support.leadingWhitespace) {
		//$(".subDealsButton").corner("3px");
	}
	startCoundownTimer();
});


function showSubDeals(el){
	foo = el;
	if($(el).hasClass('closed')){
		var id = $(el).attr('id');
		id = id.replace(/subDealsButton_/, '');

    modifyOfferBackground(id);
    modifyOfferMoredeals(id);

		$(el).text('» Deals schließen');
		$('#subDealsWrapper_'+id).slideDown(function(){
			$(el).removeClass('closed').addClass('open');
		});

	}else if($(el).hasClass('open')){
		var id = $(el).attr('id');
		id = id.replace(/subDealsButton_/, '');

  	var subDealsNumber = $(el).siblings('input').val();
		$(el).text('» Weitere Deals ('+subDealsNumber+')');

		$('#subDealsWrapper_'+id).slideUp(function(){
			$(el).removeClass('open').addClass('closed');
      //Do slide up first - looks better
      modifySubDeal( 'offer_main_center_slide', id );
      modifySubDeal( 'offer_main_left_moredeals' ,id);
		});
	}
}

function modifySubDeal( element, id ) {

  if($('#'+ element + '_'+id).hasClass('slide_on')){

    $('#'+ element + '_'+id).removeClass('slide_on').addClass('slide_off');
  }
  else if($('#'+ element + '_'+id).hasClass('slide_off')){

    $('#'+ element + '_'+id).removeClass('slide_off').addClass('slide_on');
  }
}

function modifyOfferBackground( id ) {

  if($('#offer_main_center_slide_'+id).hasClass('slide_on')){

    $('#offer_main_center_slide_'+id).removeClass('slide_on').addClass('slide_off');
  }
  else if($('#offer_main_center_slide_'+id).hasClass('slide_off')){

    $('#offer_main_center_slide_'+id).removeClass('slide_off').addClass('slide_on');
  }
}

function modifyOfferMoredeals( id ) {
    if($('#offer_main_left_moredeals_'+id).hasClass('slide_on')){

    $('#offer_main_left_moredeals_'+id).removeClass('slide_on').addClass('slide_off');
  }
  else if($('#offer_main_left_moredeals_'+id).hasClass('slide_off')){

    $('#offer_main_left_moredeals_'+id).removeClass('slide_off').addClass('slide_on');
  }
}

function startCoundownTimer(){
	var currentSeconds = new Date().getSeconds();
	var offsetSeconds = 60-currentSeconds;
	window.setTimeout('countdownTimer()', (offsetSeconds*1000));
}

function countdownTimer(){
	$('.time_number').each(function(i,el){
		var hours = $(el).text().match(/^ *[0-9]+/);
		hours = parseInt(hours);
		var mins = $(el).text().match(/[0-9]+Min$/);
		mins = parseInt(mins);
		//console.log('hours: '+hours+' - mins: '+mins);
		if(mins > 0){
			mins--;
		}else{
			if(hours > 0){
				hours--;
				mins = 59;
			}
		}
		var newText = hours+'Std '+mins+'Min';
		$(el).text(newText);
	});

	window.setTimeout('countdownTimer()', 60000);
}

/*********** Hover effect for info button on home page  ****************/
this.infoToolTip = function() {
	xOffsetInfo = -150;
	yOffsetInfo = -100;

	$("#deal_main_right_info_button").hover(function(e){
			$("#infoTooltip").show();
			$("#infoTooltip")
				.css("top", (e.pageY + yOffsetInfo) + "px")
				.css("left", (e.pageX + xOffsetInfo) + "px")
				.fadeIn("fast");
		},
		function(e) {
			$("#infoTooltip").hide();
		}
	)
	$("#deal_main_right_info_button").mousemove(function(e){

		$("#infoTooltip")
			.css("top", (e.pageY + yOffsetInfo) + "px")
			.css("left", (e.pageX + xOffsetInfo) + "px");
	});
}

$(document).ready(function(){
	infoToolTip();
});

/*********** Newsbox on homepage  ****************/
$(document).ready(function(){
	$('#newsbox_navi_news').click(function(){
		if (!$('#newsbox_navi_news').hasClass('active')) {
			$('#newsbox_navi_news').addClass('active');
			$('#newsbox_navi_deals').removeClass('active');
			$('#newsbox_news_wrapper').show();
			$('#newsbox_deals_wrapper').hide();
		}
	});
	$('#newsbox_navi_deals').click(function(){
		if (!$('#newsbox_navi_deals').hasClass('active')) {
			$('#newsbox_navi_news').removeClass('active');
			$('#newsbox_navi_deals').addClass('active');
			$('#newsbox_news_wrapper').hide();
			$('#newsbox_deals_wrapper').show();
		}
	});
});







