
// 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);
		});
	}
}

/*
 * @param 1: depth
 * @param 2: ID
 * @param 3: parentID
 * @param 4: commentID
 */
function showReplyBox(element, iDepth, iID, parentID, commentID) {
  // $('#first').find('div.here');
  //  $(".writecomment_center").removeClass("writecomment_center1 writecomment_center2 writecomment_center3").addClass("writecomment_center"+iDepth);
  // 
  // modify class to scale the reply comment box
  $('#replycomment_box').find(".writecomment_center").removeClass("writecomment_center1 writecomment_center2 writecomment_center3").addClass("writecomment_center"+iDepth);
  $('#replycomment_box').find(".writecomment_middle").removeClass("writecomment_middle1 writecomment_middle2 writecomment_middle3").addClass("writecomment_middle"+iDepth);

  //delete hidden input fields and add the new onces
  $('.form_replycomment_box').children('.hidden_repy_comment').remove(':hidden');
  $('.form_replycomment_box').append('<input class="hidden_repy_comment" type="hidden" name="commentid" value="' + commentID + '">');
  if( iDepth >= 2 ) {
    $('.form_replycomment_box').append('<input class="hidden_repy_comment" type="hidden" name="parentid" value="' + iID + '">');
  }
  else {
    $('.form_replycomment_box').append('<input class="hidden_repy_comment" type="hidden" name="parentid" value="' + parentID + '">');
  }
  $('.form_replycomment_box').append('<input class="hidden_repy_comment" type="hidden" name="id" value="' + iID + '">');

  // Show reply comment box
  $("#"+element).hide();
  $("#"+element).insertAfter('#comment_box_' + iDepth + '_' + iID);
  $("#"+element).slideDown('slow');

}

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();
		}
	});
});

/*
 * Sort Shop Drop Down Selection
 */
$(document).ready(function(){
// get the select
var $dd = $('.select_shop_form');
if ($dd.length > 0) { // make sure we found the select we were looking for

    // get the options and loop through them
    var $options = $('option', $dd);
    var arrVals = [];
    $options.each(function(){
        // push each option value and text into an array
        arrVals.push({
            val: $(this).val(),
            text: $(this).text()
        });
    });

    // sort the array by text
    arrVals.sort(function(a, b){
        return a.text == b.text ? 0 : a.text < b.text ? -1 : 1;
    });

    // loop through the sorted array and set the text/values to the options
    for (var i = 0; i < arrVals.length; i++) {
        $($options[i]).val(arrVals[i].val).text(arrVals[i].text);
    }

    $("#shop_select").prepend($('<option/>').attr('value', '0').text('Alle Shops'));

    // set the selected value back
    $('#shop_select').val(getUrlVars()["shop_select"]);
}

});

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}







