﻿var m_url = '';
var m_productName = '';
var CacheController = {
    _cacheText: new Array(),
    _cacheVideoText: new Array(),
    _cacheVideoUrl: new Array(),
    _cacheImageUrl: new Array(),
    _cachePdfUrl: new Array(),
    _cacheForumUrl: new Array(),
    _cacheRelatedVideosUrl: new Array()
}
var prodSel = null;
var initBorder = true;
var totalitems = 6;
var attrName = "class";
var minimized = true;
var newTab = true;
var setNewTab = true;
var isPlayerLoaded = false;
var videoFunctionStack = "";


function css(el, prop) {
    return parseInt($.css(el[0], prop)) || 0;
};

function initCarouselBorder(product, imageCount) {
    if (navigator.appName == "Microsoft Internet Explorer") {
        attrName = "className";
    }
    else {
        attrName = "class";
    }
    
    $('div.carouselItems').find('li').eq(0).click();

    $('div.carouselItems').find('li').each(function(i) {
        var path = $(this).children(":first").attr('src');
        var img = new Image();
        if (path.indexOf('_active') < 0) {
            path = path.replace(".png", "_active.png");
        }
        else {
            path = path.replace("_active.png", ".png");
        }
        jQuery("<img>").attr("src", path);
    });
}

function initBorderAnimation(li) {
  
   var interval = 1;


    var endAnimation = function() {     
        init = false;

        getText();
        getImageUrl();

        $('div.carouselItems').find('li').each(function(i) {
            $(this).removeClass('disabled');
        });
    }
    
    setTimeout(endAnimation, interval);
}

function borderAnim(li) {
     var interval = 1;

    var endAnimation = function() { 
        getText();      
        $('div.carouselItems').find('li').each(function(i) {
            $(this).removeClass('disabled');
        });
    }
    setTimeout(endAnimation, interval);
}

var objActive = null;

function getProduct(obj, url, productName) {

    if (!obj.is(".selected") && !obj.is(".disabled") && setNewTab) {
        
        $('div.carouselItems').find('li').each(function(i) {
            $(this).addClass('disabled');
        });

        if (objActive) {
            var temp1 = objActive.children(":first").attr("src");
            var newPath1 = temp1.replace('_active.png', '.png');
            objActive.children(":first").attr("src", newPath1)
        }

        var temp = obj.children(":first").attr("src");
        var newPath = temp.replace('.png', '_active.png');
        obj.children(":first").attr("src", newPath)

        objActive = obj;
        $('#divHeader').find('h3').attr(attrName, '');
        $('#error').hide();
        $('#divText').find('div.resultHeader').hide();
        $('#divText').find('div.resultText').hide();
        $('#divText').find('div.img').hide();
        $('#loader').show();
        m_url = url;
        m_productName = productName;
        $('#divHeader').find('h3').addClass(productName);
        prodSel = productName;
        if (initBorder) {
       
            initBorderAnimation(obj);

            initBorder = false;
        }
        else
         
            borderAnim(obj);
    }
}

function getText() {
    if (CacheController._cacheText[prodSel] == null) {
        $.ajax({
            type: "GET",
            url: m_url + "action=GetText&product=" + m_productName + "&r=" + Math.random(),
            dataType: "text",
            success: function(result) {
                CacheController._cacheText[prodSel] = result;
                SetText();
                getImageUrl();
            },
            error: function(result) {
                $('#loader').hide();
                $('#error').show();
            }
        });
    }
    else {
        SetText();
        getImageUrl();
    }
}

function SetText() {
    $('#loader').hide();
    $('#divText').find('div.resultHeader').show();
    $('#divText').find('div.resultText').show();
    var parts = CacheController._cacheText[prodSel].split(';');
    var parts2 = parts[0].split('=');
    var parts3 = parts[1].split('=');
    parts3[1] = parts3[1].replace(/~/g, "=");
    $('#divText').find('div.resultHeader').html(parts2[1]);
    $('#divText').find('div.resultText').html(parts3[1]);
}

function getImageUrl() {
    if (CacheController._cacheImageUrl[prodSel] == null) {
        $.ajax({
            type: "GET",
            url: m_url + "action=GetImageUrl&product=" + m_productName + "&r=" + Math.random(),
            dataType: "text",
            success: function(result) {
                CacheController._cacheImageUrl[prodSel] = result;
                SetImageUrl();
            }
        });
    }
    else {
        SetImageUrl();
    }
}

function SetImageUrl() {
    $('#divText').find('div.img').show();
    var partsOfName = CacheController._cacheImageUrl[prodSel].split('|');
    var wndType = (partsOfName[2] == null || partsOfName[2] == '') ? '_blank' : partsOfName[2];

    var emptyHref = (partsOfName[1] == null || partsOfName[1] == '');
    var hrefStr = emptyHref ? '#' : partsOfName[1];
    $('#divText').find('div.back img').attr('src', partsOfName[0]);
    $('#divText').find('div.front img').attr('src', partsOfName[0]);
    $('#divText').find('div.front a').attr('href', hrefStr);
    $('#divText').find('div.back a').attr('href', hrefStr);
    $('#divText').find('div.back a').attr('target', wndType);
    $('#divText').find('div.front a').attr('target', wndType);
    $('#divText').find('.linkImage').attr('src', partsOfName[0]);

    if (emptyHref) {
        $('#divText').find('div.back a').css("display", 'none');
        $('#divText').find('div.front a').css("display", 'none');
        $('#divText').find('.linkImage').css("display", 'block');
    }
    else {
        $('#divText').find('div.back a').css("display", 'block');
        $('#divText').find('div.front a').css("display", 'block');
        $('#divText').find('.linkImage').css("display", 'none');
    }

    var emptyAnim = (partsOfName[3] == null || partsOfName[3] == '');
    var animDisplay = emptyAnim ? 'none' : 'block';
    $('#divText').find('#animation').css('display',animDisplay)
    $('#divText').find('#animation').attr('src', partsOfName[3]);    

    if (navigator.userAgent.indexOf("MSIE") == -1) {
        $('#divText').find('#animation').css('display', 'none');    
    }
    else {
        
    }
}
