// conversion tag IDs
var landingPageId = "";
var subsciptionLinkId = "";
var facebookLinkId = "";
var beboLinkId = "";
var voteButtonId = "";
var lowerSubsciptionLinkId = "";
var lowerFacebookLinkId = "";
var lowerBeboLinkId = "";
var helperLinkId = "";
var subsciptionButtonId = "";

function goodie_init_callback(carousel){
	var control = jQuery("div.NavDetailExtra");
    var control_items = control.find("> p a");

    control_items.each(function (index) {
        var el = jQuery(this);

        el.bind("click", function (evt) {
            var prev_active_el = control.find("> p a.Active");
            prev_active_el.removeClass("Active"); //clear Active
            el.addClass("Active"); //add current Active
            carousel.scroll(index+1); //scroll to
			if ( (/MSIE 6\.0/).test(navigator.userAgent) && !(/MSIE 7\.0/).test(navigator.userAgent) ) {
				DD_belatedPNG.applyVML(prev_active_el.get(0));
				DD_belatedPNG.applyVML(el.get(0));
			}
            return false; //prevent default action and stop propagation
        });
    });

    //inti state when load, find control_item has Active class and scroll to the appropriated tab content
    var active_item = control.find("> p a.Active");
    var active_index = jQuery.inArray(active_item.parent().get(0), control.find("p"));

    setTimeout(function () {
        carousel.scroll(active_index+1, false);
    }, 1);
}
function goodie_animation_callback(carousel){
	var active_index = carousel.first-1;

    //Active corresponding tab
    var prev_active_el = jQuery("div.NavDetailExtra > p a.Active");
    var active_el = jQuery("div.NavDetailExtra > p").eq(active_index).find("a");

    prev_active_el.removeClass("Active");
    active_el.addClass("Active");
	if ( (/MSIE 6\.0/).test(navigator.userAgent) && !(/MSIE 7\.0/).test(navigator.userAgent) ) {
		DD_belatedPNG.applyVML(prev_active_el.get(0));
		DD_belatedPNG.applyVML(active_el.get(0));
	}
}
jQuery(document).ready(function () {
	if ( (/MSIE 6\.0/).test(navigator.userAgent) && !(/MSIE 7\.0/).test(navigator.userAgent) ) {
		DD_belatedPNG.fix(".SubTabDetail");
		DD_belatedPNG.fix(".SubTabDetail h3");
		DD_belatedPNG.fix(".SubTabs li a");
        DD_belatedPNG.fix(".SubTabs li a.Active");
		DD_belatedPNG.fix(".Thanks");
		DD_belatedPNG.fix(".FanBox img");
		DD_belatedPNG.fix(".NavDetailExtra p"); 
		DD_belatedPNG.fix(".DownloadWrapper img");
		DD_belatedPNG.fix(".ContentDetail1 img");
		DD_belatedPNG.fix(".ContentDetail2 img");
		//or DD_belatedPNG.fix(".Sample, .SampleDivLoopPNG, .SampleDivPositionPNG");
	}
    
	if ( jQuery("ul#carousel01").length > 0 ) {
		jQuery('#carousel01').jcarousel({
	        scroll: 1,
	        initCallback: goodie_init_callback,
	        onButtonAfterAnimationCallBack: goodie_animation_callback
	    });
	}
	
    triggerEBConversion(landingPageId);
    
    initVoteVideo(voteVideoUrl);

	$("ul#tabsNav > li > a").showHiddenFunc( jQuery("ul > li.Series > a") );
	
	$("#linkToLegal").bind("click",function() {
		tb_show('','#TB_inline?height=553&width=450&inlineId=popupLegal','');
   	});
	$("#linkToContact").bind("click",function() {
		tb_show('','TB_inline?height=407&width=670&inlineId=popupContact','');
   	});
	$("#linkToCredits").bind("click",function() {
		tb_show('','TB_inline?height=453&width=803&inlineId=popupCredit','');
   	});
	//$(".LinkToSubsrible").bind("click",function() {
	//	tb_show('','TB_inline?height=483&width=614&inlineId=popupSubscribe','');
   	//});
	$("#subcriptionLink").bind("click",function() {
        triggerEBConversion(subsciptionLinkId);
		tb_show('','TB_inline?height=483&width=614&inlineId=popupSubscribe','');
   	});
	$("#lowerSubcriptionLink").bind("click",function() {
        triggerEBConversion(lowerSubsciptionLinkId);
		tb_show('','TB_inline?height=483&width=614&inlineId=popupSubscribe','');
   	});
	$("#linkToLanguage").bind("click",function() {
		tb_show('','TB_inline?height=290&width=680&inlineId=popupLanguage','');
   	});
	$("#linkToEpisodes").bind("click",function() {
		tb_show('','TB_inline?height=400&width=1024&inlineId=popupEpisodes','');
   	});
	$("#subscriberBt").bind("click",function() {
		return saveSubscriber();
   	});
	$("#sendBtn01").bind("click",function() {
		return sendVote();
   	});
	$("#episodeBSwitcher").bind("click",function() {
		switchEpisodeVideo('B');
   	});
	$("#episodeASwitcher").bind("click",function() {
		switchEpisodeVideo('A');
   	});
	$("#facebookLink").bind("click",function() {
		triggerEBConversion(facebookLinkId);
   	});
	$("#beboLink").bind("click",function() {
		triggerEBConversion(beboLinkId);
   	});
	$("#lowerFacebookLink").bind("click",function() {
		triggerEBConversion(lowerFacebookLinkId);
   	});
	$("#lowerBeboLink").bind("click",function() {
		triggerEBConversion(lowerBeboLinkId);
   	});
	$("#helperLink").bind("click",function() {
		triggerEBConversion(helperLinkId);
   	});
	$(".ThickboxCloseLink").bind("click",function() {
		tb_remove();
        return false;
   	});
	$("#subscriptionCloseLink").bind("click",function() {
		resetSubscriberForm();
        tb_remove();
        return false;
   	});
    
    
	$('#mentionsLegales').jScrollPane();
	$('.ChooseType > label').each(function () {
		var handle = $(this);
		var target_content = $(handle.attr("rel"));
		handle.bind("mousemove", function () {
			if ( ! $(this).hasClass("Active") && ! $(this).hasClass("Disable") ) {
				$("#btnSubmit").addClass("TabContentHidden");
				$("#normalState").addClass("TabContentHidden");
				if ( target_content.hasClass("TabContentHidden") ) {
					$('.ChooseType > label').each(function () {
						$($(this).attr("rel")).addClass("TabContentHidden");
						$(this).removeClass("Active");
						$(this).removeClass("Hover");
					});
					target_content.removeClass("TabContentHidden");
					$(this).addClass("Hover");
				}
			}
			return false;
		});
		handle.bind("click", function () {
			if ( ! $(this).hasClass("Active") && ! $(this).hasClass("Disable")) {
				$("#normalState").addClass("TabContentHidden");
				$('.ChooseType > label').each(function () {
					$($(this).attr("rel")).addClass("TabContentHidden");
					$("#" + $(this).attr("for")).attr('checked',false);
					$(this).removeClass("Active");
					$(this).removeClass("Hover");
				});
				$("#btnSubmit").removeClass("TabContentHidden");
				$("#" + $(this).attr("for")).attr('checked',true);
				$(this).addClass("Active");
				$(this).addClass("Hover");
			}
			return false;
		});
		handle.bind("mouseout", function () {
			if ( ! $(this).hasClass("Active") && ! $(this).hasClass("Disable") ) {
				$("#btnSubmit").addClass("TabContentHidden");
				if ( ! target_content.hasClass("TabContentHidden") ) {
					$('.ChooseType > label').each(function () {
						$($(this).attr("rel")).addClass("TabContentHidden");
					});
					$("#normalState").removeClass("TabContentHidden");
					$(this).removeClass("Hover");
				}
			}
			return false;
		});
	});
});
jQuery.fn.extend({
	showHiddenFunc: function (remote_control) {
		//core
		$showHide = function (jEl, remote_control) {
			jEl.each(function () {
				var handle = $(this);
                var target_content = $(handle.attr("href"));
				if ( handle.hasClass("Active") ) {
					target_content.removeClass("TabContentHidden");
				}
				handle.bind("click", function () {
					if ( target_content.hasClass("TabContentHidden") ) {
						jEl.each(function () {
							$($(this).attr("href")).addClass("TabContentHidden");
							$(this).removeClass("Active");
						});
						handle.addClass("Active");
						target_content.removeClass("TabContentHidden");
					}

                    return false;
				});	
			});

            if ( remote_control != undefined ) {
					jEl.each(function () {
                        if ( remote_control.attr("href") == $(this).attr("href") ) {
                            var same_instance = $(this);
                            remote_control.bind("click", function () {
                                same_instance.trigger("click");
                            });
                        }
                    });
            }
		}
		//setup
		new $showHide($(this), remote_control);
	}
});