$(document).ready(function(){
 var content, $element, $flipStart = 0, $contentIndex, $menuPromotion,
     $parent = $('.mod-promotionBox #LoopThroughTitles .promoTabs'),
     $content = $('.mod-promotionBox #LoopThroughContent .preview'),
     $tab = 0,
     $TabBoxTimeInterval = !isNaN($("input[name='TimeInterval']").val()) ? $("input[name='TimeInterval']").val() * 1000 : 0,
     $flipContent = $("#LoopThroughContent"),
     $flipTab = $("#LoopThroughTitles"),
     $determineSwitch = $parent.length,
     $determineSwitchContent = $content.length;

if($determineSwitch > 1){
    $parent.each(function(index){
	 var $link = $(this).find("a"),
	     $parentIndex = index;
	 $link.click(function(){
	    $content.hide();
	    $content.each(function(index){
		$contentIndex = index;
		$contentIndex == $parentIndex ? $(this).show() : $(this).hide();
	    });
	    $parent.find("a").addClass("inactiveTab");
	    $(this).removeClass("inactiveTab");
	    return false;
	 });
     });
 }


function getChildren(content){
    content = content.find(".preview");
    return content;
}

function flipperStart(){
    $element = getChildren($flipContent);
    $menuPromotion = $flipTab.find('.PromoMenuTab');

    //check if the tabs above are clicked manually. if not, get default, as stated above.
    $tab = isNaN($contentIndex) ? $tab : $contentIndex;

    if ($TabBoxTimeInterval > 0){
	$flipStart = setInterval(
	function(){
	    //hide all
	    $element.hide();
	    $menuPromotion.addClass('inactiveTab');

	    //show only one
	    $element.eq($tab).show();
	    $menuPromotion.eq($tab).removeClass('inactiveTab');

	    //tab switch
	    $tab = $tab == 0 ? 1 : 0
	}, $TabBoxTimeInterval);
    } else {
	$flipStart = setInterval(function(){
	    //do nothing
	},$TabBoxTimeInterval);
    }
}

function flipperCancel(){
    clearInterval($flipStart);
}

if($determineSwitch > 1){
    $(".PromoTabs").hover(function(){
	//full stop
	flipperCancel();
    },function(){
	//restart
	flipperCancel();
	flipperStart();
    });
    //start
    flipperStart();
}

    function jsonpromotest(){
	var test = -1;
	if(typeof(promotions) == 'object' && typeof(pickedPromotion) == "object"){
	    if(typeof(pickedPromotion.imageURL) != 'undefined'){
			test = pickedPromotion.imageURL.indexOf('.jpg') < 0 ? -1 : 1;
			test = test > 0 ? 1 : pickedPromotion.imageURL.indexOf('.jpeg') < 0 ? -1 : 1;
			test = test > 0 ? 1 : pickedPromotion.imageURL.indexOf('.gif') < 0 ? -1 : 1;
			test = test > 0 ? 1 : pickedPromotion.imageURL.indexOf('.png') < 0 ? -1 : 1;
			test = test > 0 ? 1 : pickedPromotion.imageURL.indexOf('.bmp') < 0 ? -1 : 1;
			if(typeof(pickedPromotion.outclickURL) != 'undefined'){
				return test;
			}
	    }
	    return test;
	}
	return test;
    }

    //if div container is present for the promotion box and has a valid image, get its height and make the tabbed content box the same
    if(typeof($("#PromotionBox")) == 'object' && jsonpromotest() > 0){
	var $first = 0,
	    $finalHeight = 0;

	$first = typeof($("img#promo-spot-banner-img")) == "object" ? $("img#promo-spot-banner-img").height() : $first;
	$finalHeight = $first;

	$("img#promo-spot-banner-img").load(function(){
	    //get Image height as a file, not as a element on the page, to get accurate height, then add the padding-top on the div.
	    $("img#promo-spot-banner-img").removeAttr("width");
	    $("img#promo-spot-banner-img").removeAttr("height");
	    $first = $("img#promo-spot-banner-img").height();
	    $finalHeight = $first;

	    //add height to all
	    $("#LoopThroughContent .previewBlog,#LoopThroughContent .previewNews,#PromotionBox .previewBestOffer").css('height',$finalHeight + "px");
	    $(".promotionBoxPreview .promotionBoxBottom").css({"position":"absolute","bottom":0});
	});

	$("#PromotionBox").css('display','block');
	$(".mod-promotionBox").removeClass("mod-promotionBox-wide");

    }
});
