$(document).ready(function() {
	
	$('td.comparesets').click(function() {
		if ($(this).find('a').hasClass('whitearrow_right')) {
			$(this).find('a').removeClass('whitearrow_right').addClass('whitearrow_down');
			$('table.sets > tbody').show();
		} else {
			$(this).find('a').removeClass('whitearrow_down').addClass('whitearrow_right');			
			$('table.sets > tbody').hide();
		}
	});

	$('td.openproducts').click(function() {
		var id = $(this).attr('id');
		if ($(this).find('a').hasClass('blackarrow_right')) {
			$(this).find('a').removeClass('blackarrow_right').addClass('blackarrow_down');
			$('tr.'+id+'_content').show();
			$('tr.'+id+'_whiteline').hide();
		} else {
			$(this).find('a').removeClass('blackarrow_down').addClass('blackarrow_right');			
			$('tr.'+id+'_content').hide();
			$('tr.'+id+'_whiteline').show();
		}
	});
	
	$('div.gototop > a').click(function() {
		jumpto('body');
	});
	
	var animationInProgress = false;
	
	$('td.comparesets').bind('mouseenter', function() {
		if (animationInProgress==false) {
			animationInProgress = true;
			$(this).animate( {backgroundColor:"#555555"}, 150, function() {
				animationInProgress = false;
			});
		}
	});
	
	$('td.comparesets').bind('mouseleave', function() {
		animationInProgress = false;
		$(this).animate( {backgroundColor:"#444444"}, 150);
	});
	
	$('td.openproducts').bind('mouseenter', function() {
		if (animationInProgress==false) {
			animationInProgress = true;
			$(this).animate( {backgroundColor:"#D3D3D3"}, 150, function() {
				animationInProgress = false;
			});
		}
	});
	
	$('td.openproducts').bind('mouseleave', function() {
		animationInProgress = false;
		$(this).animate( {backgroundColor:"#E5E5E5"}, 150);
	});
	
	$('.cclink,#ccteaserlink').click(function(){
  var ean = $(this).attr("href").replace(/\D/g,'');
		cc_layer(ean);
		$(this).blur();
		return false;
	});
	$('.cclink').each(function(){
		//$(this).html('');
		var ean = $(this).attr('href').replace(/\D/g, '');
		if (typeof(shops[ean]) != "undefined" && shops[ean] > 0) {
			$(this).css("display", "block");
		}
	});
	$('#ccleft').click(function() {
		var ml = parseInt($('#cclayercontent').css("margin-left").replace(/px/, ""));
		if (ml < 0) {
			ml += cc_step;
			ml = ml > 0 ? 0 : ml;
			//$('#cclayercontent').css("margin-left", ml + "px");
			$('#cclayercontent').animate({marginLeft: ml + "px"});
		}
		$(this).blur();
		return false;
	});
	$('#ccright').click(function() {
		var ml = $('#cclayercontent').css("margin-left").replace(/px/, "");
		if (isNaN(ml)) { ml = 0; }
		var w = parseInt($('#cclayercontent').css('width').replace(/px/, ""));
		var v = $('#cchtml').width();
		var limit = -1 * (w - v - 5);
		if (ml > limit) {
			ml -= cc_step;
			ml = ml < limit ? limit : ml;
			//$('#cclayercontent').css("margin-left", ml + "px");
			$('#cclayercontent').animate({marginLeft: ml + "px"});
		}
		$(this).blur();
		return false;
	});
	$('#ccclose, div#black').click(function() {
		$('#cclayer').fadeOut(); 
		$('div#black').fadeOut();
		return false;
	})
});

$(document).click(function(e){
	if ($('#cclayer').is(':visible')) {
		var ccpos = $('#cclayer').offset();
		
		var x1 = ccpos.left - 10;
		var x2 = ccpos.left + $('#cclayer').width() + 10;
		
		var y1 = ccpos.top - 10;
		var y2 = ccpos.top + $('#cclayer').height() + 10;
		
		if (e.pageX >= x1 && e.pageX <= x2 && e.pageY >= y1 && e.pageY <= y2) {
			// klick ins popup = kein effekt
		}
		else {
			$('#cclayer').fadeOut();
		}
		
	}
});


function jumpto(element) {
	$.scrollTo($(element), 800);
}

// werte werden normalerweise automatisch berechnet / angepasst
var cc_step = 180;
var cc_elements = 2;
function cc_layer(ean) {
	
	$('div#black').css('width', $(document).width() ).css('height', $(document).height() ).css('opacity','0.7').fadeIn();
	
	$('#cclayer').css("background-image", "url(/multimaster/campaign_data/img/loading.gif)");
	$('#cchtml').html('');
	$('#ccleft').hide();
	$('#ccright').hide();
	$('#cclayercontent').css("margin-left", "0px");
	if (!$('#cclayer').is(":visible")) {
		$('#cclayer').fadeIn();
	}
	$.ajax({
		type: "POST",
		url: "/multimaster/campaign_data/cc.php",
		data: "ean=" + ean + "&lang=" + lang,
		success: function(html){
			if (html == -1) {
				alert("Keine Shops gefunden");
				$('#cclayer').fadeOut();
				$('#black').fadeOut();
				
				return;
			}
			$('#cclayercontent').css("margin-left", "0px");
     		$('#cclayer').css("background-image", "none");
			$('#cchtml').html(html);
			$('.ccshop').css("left", "0px");
			
			// schrittweite und sichtbare shops automatisch berechnen
			cc_step = parseInt($('.ccshop').css('width').replace(/px/, ''));
			if ($('.ccshopspacer').length) { 
				cc_step +=  parseInt($('.ccshopspacer').css('width').replace(/px/, '')); 
			}
			var v = $('#cchtml').width();
			var cc_elements = Math.round(v / cc_step);
			
			if ($('.ccshop').length < cc_elements) {
				var w = parseInt($('.ccshop').css('width').replace(/px/, ""));
				var v = $('#cchtml').width();
				var l = Math.round((v - w) / 3);
				//$('.ccshop').css("position", "relative");
				//$('.ccshop').css("left", l + "px");
			}
			
			var w = parseInt($('#cclayercontent').css('width').replace(/\D/, ''));
			if ($('.ccshop').length > cc_elements) {
				$('#ccleftblank').hide();
				$('#ccrightblank').hide();
				$('#ccleft').show();
				$('#ccright').show();
			}
			else {
				$('#ccleft').hide();
				$('#ccright').hide();
				$('#ccleftblank').show();
				$('#ccrightblank').show();
			}

      // Breite des ShopFenster automatisch berechnen, damit die Shops nicht nach unten umbrechen sondern durchgescrollt werden können.
			var cc_width = ($('.ccshop').length*$('.ccshop').width()) + (($('.ccshop').length-1)*$('.ccshopspacer').width());
			$('#cclayercontent').css('width', cc_width+'px');
			
		}
	});
}
