//var TabClicked = false; //Wegen Lightbox auskommentiert

$(document).ready(function() {
    /*
    $$('a.withtooltip').each(function(element, index) {
    var content = element.get('title').split('::');
    element.store('tip:title', content[0]);
    element.store('tip:text', content[1]);
    });

    //create the tooltips
    var ToolTips = new Tips('.withtooltip', {
    className: 'withtooltip',
    fixed: true,
    hideDelay: 20,
    showDelay: 30,
    offsets: { 'x': -20, 'y': 25 }
    });*/    
    addPlayers();
    InitalizeTabs();
    activateFirstTab();
});

//$(document).ready(function() {
    //NormalizeTabDisplaysHeight();    
//});

function AddSlider(Button, SlideContainer, DefaultStartingStatus) {
    var Slide = $(SlideContainer);

    $(Button).click(
        function() {
            Slide.slideToggle(
                function() {
                    $(Button).toggleClass('btn_closed');
                    $(Button).toggleClass('btn_opened');
                    //Cookie.write($(SlideContainer).attr('Id'), Slide.open);

                    var options = { path: '/', expires: 10 };
                    var status = $(Button).hasClass('btn_opened');
                    $.cookie($(SlideContainer).attr('Id'), status, options);
                }
            );
        }
    );
            
    statusCookie = $.cookie($(SlideContainer).attr('Id'));

    if (statusCookie != null) { //restore status from cookies        
        if (statusCookie == 'true') { //Cookies are stored as strings
            Button.addClass('btn_opened');
        } else {
            Slide.slideUp(0);
            Button.addClass('btn_closed');
        }
    } else { //use the parameters given by the call of the function
        if (DefaultStartingStatus) {
            Button.addClass('btn_opened');
        } else {
            Slide.slideUp(0);
            Button.addClass('btn_closed');
        }
    }
}

function InitalizeTabs() {
    $('#PreviewPage_TabContainer a[id^=tab_btn_]').each(function() {        
        var buttonId = $(this).attr('id');
        AddTab($('#PreviewPage_TabContainer'), $('#PreviewPage_Tab_Displays'), buttonId);
    });
}

function AddTab(TabsContainer, TabDisplays, Default) {
    $(TabsContainer).find('a').click(function(e) {
        try {
            $(TabsContainer).find('.selected').removeClass('selected');
            $(TabDisplays).find('.selected').removeClass('selected');
        } catch (e) { }
        $(this).addClass('selected');
        $('#' + $(this).attr('id').replace("tab_btn_", "tab_display_")).addClass('selected');        
    });
}

function NormalizeTabDisplaysHeight() {
    var Displays = $('#PreviewPage_Tab_Displays div');
    var maxHight = 0;
    for (var i = 0; i < Displays.length; i++) {
        try {
            if ($(Displays[i]).attr('id').substring(0, 12) == 'tab_display_') {
                //Reset height                    
                $(Displays[i]).css('height', 'auto');
                $(Displays[i]).css('display', 'block');
                if ($(Displays[i]).height() > maxHight) {
                    maxHight = $(Displays[i]).innerHeight();
                }
                $(Displays[i]).css('display', null);
            }
        }
        catch (e) { }
    }
    for (var i = 0; i < Displays.length; i++) {
        try {
            if ($(Displays[i]).attr('id').substring(0, 12) == 'tab_display_') {
                $(Displays[i]).css('height', maxHight + 'px');
            }
        }
        catch (e) { }
    }
}

function resizePriceCalculatorIFrame(PriceCalcSlider) {    
    /*
    if (PriceCalcSlider.open) {
        PriceCalcSlider.hide();
        PriceCalcSlider.show();
    } else {
        PriceCalcSlider.hide();
        PriceCalcSlider.show();
        PriceCalcSlider.hide();
    }*/
}

function OpenPriceCalc(Id) {
    window.open("pricecalc.asp?image=" + Id, "calcprice", "scrollbars=yes,resizable=yes,directories=no,toolbar=no,menubar=no,top=10,left=10");
}

function DirectDownload3(ImageId, Resolution) {
    window.open("download3.asp?image=" + ImageId + "&res=" + Resolution, "download3", "scrollbars=yes,resizable=yes,directories=no,toolbar=no,menubar=no,height=300,width=550,top=10,left=10");
}

function DownloadFootageLayout(ImageId, Res) {
    window.location = "yourimage.asp?image=" + ImageId + "&res=" + Res;
}

function AddAssetToCart(ImageId, res) {
    if (window.conf_addToCartWithClient === undefined) {
        var addToCartWithClient = false
    } else {
        addToCartWithClient = conf_addToCartWithClient
    }

    if (addToCartWithClient) {
        addToCartWithEndClient(ImageId, res);
    } else {
        addToCartWithoutClient(ImageId, res);
    }
}

function addToCartWithEndClient(ImageId, res) {
    $("#EndClient input[name=image]").attr('value', ImageId);
    $("#EndClient input[name=size]").attr('value', res);

    $("#EndClient").dialog({
        modal: true,
        height: 160,
        buttons: {
            'OK': function() {
                var customer = $("#EndCustomerInput").val();
                if (customer == "") {
                    alert(lang_PleaseEnterCustomer);
                } else {
                    $(this).dialog('close').dialog('destroy');
                    $.post(
                            "ajax_AddOneToCart.asp",
                            {
                                rfaddtocart: res,
                                image: ImageId,
                                EndCustomer: customer
                            }
                        );
                }
            },
            'Abbrechen': function() {
                $(this).dialog('close').dialog('destroy');
            }
        }
    });
}

function addToCartWithoutClient(ImageId, res) {
    window.open('pricecalc.asp?image=' + ImageId + '&rfaddtocart=' + res + '&rfformflag=true&DoNotClose=true', 'Warenkorb', "width=600,height=400,status=yes,scrollbars=yes,resizable=yes");
}

function activateFirstTab() {
    $('#PreviewPage_TabContainer').find(':first-child').find(':first-child').find(':first-child').addClass('selected');
    $('#PreviewPage_Tab_Displays').find(':first-child').addClass('selected');
}

function showMainPlayer(MoviePath, PreviewPath, ContainerId, withSound) {
    startClip = StartDirectly();
    
    $f(ContainerId, { src: FlowPlayerSWFPath, wmode: 'transparent' }, {
        key: FlowPlayerLicenseKey,
        clip: {
            scaling: 'fit',
            autoBuffering: false,
            autoPlay: false
        },
        canvas: { backgroundColor: "#FFFFFF" },
        plugins: {
            controls: {
                bufferGradient: 'none',
                buttonColor: '#8d1c22',
                timeColor: '#ffffff',
                borderRadius: '0',
                durationColor: '#ffffff',
                progressColor: '#8d1c22',
                backgroundGradient: 'none',
                progressGradient: 'medium',
                backgroundColor: '#D9E3EC',
                sliderGradient: 'none',
                buttonOverColor: '#8d1c22',
                sliderColor: '#000000',
                bufferColor: '#8d6769',
                opacity: 1.0,
                play: true,
                volume: withSound,
                mute: withSound ,
                time: true,
                stop: true,
                playlist: false,
                fullscreen: true
            }
        },
        playlist: [
                { url: PreviewPath, autoPlay: true },
                { url: MoviePath, autoPlay: startClip }
            ]
    });
}

function addPlayers(){
    $('#PreviewPage_Tabs div.TabFootageContainer').each(function(el) {
        var AssetId = ExtractAssetIDFromElementByUrl(this);
        var ThumbUrl = ExtractThumbUrl(this);
        var ContainerID = $(this).attr('id');
        $(this).empty();
        Player = showThumbPlayer(AssetId, ContainerID, FlowPlayerSWFPath, ThumbUrl);
    });
}

function ExtractThumbUrl(element) {
    var Url = $(element).find('a:first-child').css('background-image');    
    Url = Url.replace('url\(', '');
    Url = Url.replace('\)', '');
    Url = Url.replace('"', '');
    Url = Url.replace('"', '');
    return Url;
}

function ExtractAssetIDFromElementByUrl(element) {
    Url = $(element).find('a:first-child').attr('href')
    var Ausdruck = /(\d{6})/;
    Ausdruck.exec(Url);
    return RegExp.$1;
}

function getFootageThumbPath(AssetId) {
    var temp = '/Footage/' + AssetId.substring(0, 2);
    temp += '/';
    temp += AssetId.substring(2, 4);
    temp += '/';    
    temp += AssetId + '_t.flv';
    return temp;
}

function showThumbPlayer(AssetId, ContainerId, FlowPlayerSWFPath, ImagePath) {
    var MoviePath = getFootageThumbPath(AssetId);
        
    var player = $f(ContainerId, { src: FlowPlayerSWFPath, wmode: 'transparent' }, {
        key: FlowPlayerLicenseKey,
        clip: {
            scaling: 'fit',
            autoBuffering: true,
            linkUrl: 'results.asp?image=' + AssetId
        },
        canvas: { backgroundColor: "#FFFFFF" },
        plugins: { controls: null },
        playlist: [
                { url: ImagePath , autoPlay: true },
                { url: MoviePath, autoPlay: false, autoBuffering: true, loop : true }
            ],
        onMouseOver: function() {
            this.play();
        },
        onMouseOut: function() {
            this.pause();
        }
    });
    return player;
}

function enableAllRFShoppingCartIcons() {
    $('#PreviewPage_Prices_Slider a.ShoppingCartEnabled').css('display', 'inline' );
    $('#PreviewPage_Prices_Slider img.ShoppingCartDisabled').css('display', 'none' );
}

function disableRFFirstShoppingCartIcons() {
    ShoppingCartEnabledIcons = $('#PreviewPage_Prices_Slider a.ShoppingCartEnabled');
    ShoppingCartDisabledIcons = $('#PreviewPage_Prices_Slider img.ShoppingCartDisabled');

    for (var j = 0; j <= ShoppingCartEnabledIcons.length - 1; j++) {
        $(ShoppingCartEnabledIcons[j]).css('display', 'none');
        $(ShoppingCartDisabledIcons[j]).css('display', 'inline');
    }

    $(ShoppingCartEnabledIcons[ShoppingCartEnabledIcons.length - 1]).css('display', 'inline');
    $(ShoppingCartDisabledIcons[ShoppingCartEnabledIcons.length - 1]).css('display', 'none');    
}

function disableShoppingCartIconsAtBeginn() {
    $('#PreviewPage_Prices_Slider a.ShoppingCartEnabled').css('display','none');    
}

function setRFShoppingCartIcons() {
    if ($('#radioRFConstraintYes').is(':checked')) {
        disableRFFirstShoppingCartIcons();
    }

    if ($('#radioRFConstraintNo').is(':checked')) {
        enableAllRFShoppingCartIcons();        
    }
}

function StartDirectly() {
    var location = String(window.location);
    var regex = /play=/
    return regex.test(location);
    //return location.test("play=");
}
