﻿var MarketNews = new Array();
var MarketNewsIndex = 0;

var ah;// = document.getElementById("facewidget-a");
var bg; //= document.getElementById("facewidget-bg");
var img; // = document.getElementById("facewidget-img");
var low; //= document.getElementById("facewidget-lowslider");
var quote; //= document.getElementById("facewidget-quote");
var quotetext; //= document.getElementById("facewidget-quote-text");
var hider; // = document.getElementById("facewidget-hider");
var lowtext; // = document.getElementById("facewidget-lowslider-text");
var IE; //= detectIE6();

$(document).ready(function () {
    ShowNews();
    //ShowImportantNews();

     ah = document.getElementById("facewidget-a");
     bg = document.getElementById("facewidget-bg");
     img = document.getElementById("facewidget-img");
     low = document.getElementById("facewidget-lowslider");
     quote = document.getElementById("facewidget-quote");
     quotetext = document.getElementById("facewidget-quote-text");
     hider = document.getElementById("facewidget-hider");
     lowtext = document.getElementById("facewidget-lowslider-text");
     IE = detectIE6();

     initFaceWidget(widgetfeed);

});

function ShowTitle() {
    var NewsItem = MarketNews[MarketNewsIndex];
    $("#animated-title").html("<a target='_blank' style='text-decoration:none;color:#000000' href='" + NewsItem.Url + "'>" + NewsItem.Title + "</a>");
    $("#animated-title").parent().parent().css("background", "url(" + NewsItem.ImageLink + ") no-repeat");
    $("#animated-title").parent().animate({ left: 0 }, 1000, function () {
        $("#animated").animate({ left: 0 }, 800, function () {
            $("#animated").animate({ top: -50, height: 30 }, 500, function () {
                $("#animated").text(NewsItem.Category);
                $(document).oneTime(4000, HideTitle);
            });
        });
    });
}

function HideTitle() {
    $("#animated").text("");
    $("#animated").animate({ top: -40, height: 10 }, 500, function () {
        $("#animated").animate({ left: -300 }, 1000, function () {
            $("#animated").text("");
            $("#animated-title").parent().animate({ left: -380 }, 1000, function () {
                $("#animated-title").html("");
                MarketNewsIndex++;
                if (MarketNewsIndex > MarketNews.length - 1) {
                    MarketNewsIndex = 0;
                    ShowImportantNews();
                }
                else
                    $(document).oneTime(1000, ShowTitle);
            });
        });
    });

}

function SetNewsTimer() {
    $("#news").sliding({
        showItems: 7,
        url: Action("News", "GetUnreadNews"),
        rowBinder: RowBinder
    });
//    SetTimerEx(
//        5000,
//        $("#news"),
//        Action("News","GetUnreadNews"),
//        function (item) { return "<table><tr><td style='padding-top:5px;vertical-align:top;text-align:right;width:50px;font-size:11px'>" + item.Date + "</td><td style='padding-top:5px;width:230px;font-size:11px;color:#795709;'>" + item.Title + "<td></tr></table>"; }
//        ,false,false,false,"sliding");
}

function RowBinder(item, old) {
    var _new = "";
    if (!old) _new = " class='new'";
    return "<tr><td style='vertical-align:top;text-align:right;width:50px;font-size:11px'>" + item.Date + "</td><td style='padding-left:10px;vertical-align:top;'><a target='_blank' href='" + item.Url + "' style='width:180px;font-size:11px;color:#795709;text-decoration:none'>" + item.Title + "</a></td></tr>";
    
    //return "<li" + _new + " style='list-style-type:none;height:30px;'><div><tr><td style='vertical-align:top;text-align:right;width:50px;font-size:11px'>" + item.Date + "</td><td style='padding-left:10px'><a href='" + item.Url + "' style='width:180px;font-size:11px;color:#795709;text-decoration:none'>" + item.Title + "</a></td></tr></table></li>";
}

function ShowNews() {
    var _xhr = $.post(Action("News", "GetRecentNews"),
    { Count: 7 },
    function (res) {
        if (res.Error) { }
        else {
            $("#news").html("<table></table>");
            for (var i = 0; i < res.News.length; i++) {
                $("#news table").append(RowBinder(res.News[i], true));
            }
            $("#news table").append("<tr><td></td><td style='padding-left:10px;'><a target='_blank' href='http://exp.idk.ru/news' style='width:180px;font-size:11px;color:#795709;text-decoration:underline'>Все новости</a></td></tr>")

        }
    });
    XHRS.push(_xhr);
}

function ShowImportantNews() {
    MarketNews = new Array();
    var _xhr = $.post(Action("News","GetImportantNews"),
    { Count: 5 },
    function (res) {
        if (res.Error) alert(res.Error);
        else {
            for (var i = 0; i < res.News.length; i++) {
                MarketNews.push(res.News[i]);
            }
        }
        ShowTitle();
    });
    XHRS.push(_xhr);
}
/*
* Скрипт взят из exp.idk.ru
* 
*/

function detectIE6() {
    var browser = navigator.appName;
    if (browser == "Microsoft Internet Explorer") {
        var b_version = navigator.appVersion;
        var re = /\MSIE\s+(\d\.\d\b)/;
        var res = b_version.match(re);
        if (res[1] <= 6) {
            return true;
        }
    }
    return false;
}

var QUOTETOPBASE = -536;
if (navigator.userAgent.indexOf("MSIE") != -1) {
    QUOTETOPBASE = -530;
}

function processWidgetFeed(source, index) {
    if (index > source.length - 1) { index = 0 }
    ah.href = source[index].href;
    //var bgi = index > 0 ? index - 1 : source.length - 1;
    //index > 0 ? bgi = index - 1 : bgi = source.length - 1;
    //alert(bgi)
    //bg.src = source[bgi].img;
    img.style.opacity = 0;
    img.src = source[index].img;
    img.width = '356px';
    img.height = '228px';
    img.style.filter = "alpha(opacity=0)"
    if (navigator.userAgent.indexOf("MSIE") != -1 && document.documentMode && document.documentMode == 8 || IE) {
        jQuery(hider).click(function () { document.location.href = source[index].href; });
    }
    low.style.left = "-356px";
    lowtext.innerHTML = source[index].text;
    quotetext.innerHTML = source[index].quote;
    quotetext.style.top = "-500px";

    quote.style.top = (QUOTETOPBASE + quotetext.offsetHeight / 2) + "0px";
    if (navigator.userAgent.indexOf("MSIE") != -1 && document.documentMode && document.documentMode == 8) {
        quotetext.style.paddingBottom = '20px';
    }
    if (navigator.userAgent.indexOf("MSIE") != -1 && document.documentMode && document.documentMode == 8 || IE) {
        quote.style.top = parseFloat(quote.style.top) - 6 + "px";
    }
    
    quote.style.height = "1px";
    quote.style.left = -356 + "px";
    /*ShowState();*/
    var t = window.setTimeout(function () { showImage(t, index, 0) }, 50)
}

function ShowState() {
//    s = "quote: " + quote.style.top + "<br/>";
//    s += "quotetext: " + quotetext.style.top + "<br/>";
//    s += "low: " + low.style.top + "</br>";
//    s += "lowtext: " + lowtext.style.top + "</br>";
//    s += "hider: " + hider.style.top + "</br>";
//    $("#span").html(s);
}

function showImage(t, index, iteration) {
    if (iteration == 10) {
        window.clearTimeout(t);
        bg.src = widgetfeed[index].img;
        t = window.setTimeout(function () { slideIn(t, index, 0) }, 50);
    }
    else {
        img.style.opacity = (iteration + 1) * 0.1;
        var o = img.style.opacity;
        img.style.filter = "alpha(opacity=" + o * 100 + ")";
        window.setTimeout(function () { showImage(t, index, iteration + 1) }, 50)
    }
}


function slideIn(t, index, iteration) {
    /*ShowState();*/
    
    if (iteration == 20) {
        window.clearTimeout(t);
        if (navigator.userAgent.indexOf("MSIE") != -1 && document.documentMode && document.documentMode == 8 || IE) {
            bg.style.top = "-228px";
        }
        quotetext.style.top = 0;
        quotetext.style.opacity = 0;
        quotetext.style.filter = "alpha(opacity=0)"
        //var letters = quoteLetterize();
        t = window.setTimeout(function () { typeQuote(t, index, 0/*, letters*/) }, 200)
    }
    else {
        low.style.left = ((19 - iteration) * (-356 / 20)) + "px";
        if (iteration < 10) {
            quote.style.left = ((9 - iteration) * (-356)) / 10 + "px";
        }
        else {
            quote.style.height = (1 + iteration) / 20 * (quotetext.offsetHeight - 2) + "px";
            quote.style.top = QUOTETOPBASE + (19 - iteration) / 20 * quotetext.offsetHeight / 2 + "px";
            if (navigator.userAgent.indexOf("MSIE") != -1 && document.documentMode && document.documentMode == 8 || IE) { quote.style.top = parseFloat(quote.style.top) - 6 + "px" }
            if (IE) { quote.style.top = parseFloat(quote.style.top) - 4 + "px" }
        }
        window.setTimeout(function () { slideIn(t, index, iteration + 1) }, 30)
    }
}


function slideOut(t, index, iteration) {
    /*ShowState();*/

    if (iteration == 20) {
        window.clearTimeout(t);
        processWidgetFeed(widgetfeed, index + 1);
    }
    else {
        low.style.left = ((1 + iteration) * (-356 / 20)) + "px";
        if (iteration < 10) {
            quote.style.height = (9 - iteration) / 10 * (quotetext.offsetHeight - 2) + "px";
            quote.style.top = QUOTETOPBASE + (1 + iteration) / 10 * quotetext.offsetHeight / 2 + "px";
            if (navigator.userAgent.indexOf("MSIE") != -1 && document.documentMode && document.documentMode == 8 || IE) { quote.style.top = parseFloat(quote.style.top) - 6 + "px" }
        }
        else {
            quote.style.left = ((1 + iteration) * (-356)) / 20 + "px";
        }
        window.setTimeout(function () { slideOut(t, index, iteration + 1) }, 30)
    }
}

function textSlideIn(t, index, iteration) {
    /*ShowState();*/
    if (iteration == 10) {
        window.clearTimeout(t);
        t = window.setTimeout(function () { quoteSlideIn(t, index, 0) }, 800);
    }
    else {
        low.style.left = ((9 - iteration) * (-356 / 10)) + "px";
        window.setTimeout(function () { textSlideIn(t, index, iteration + 1) }, 50);
    }
}

function quoteSlideIn(t, index, iteration) {
    /*ShowState();*/
    if (iteration == 10) {
        window.clearTimeout(t);
        t = window.setTimeout(function () { quoteExpand(t, index, 0), 200 })
    }
    else {
        quote.style.left = ((9 - iteration) * (-356)) / 10 + "px";
        window.setTimeout(function () { quoteSlideIn(t, index, iteration + 1) }, 10);
    }
}

function quoteExpand(t, index, iteration) {
    /*ShowState();*/
    if (iteration == 10) {
        window.clearTimeout(t);
        if (navigator.userAgent.indexOf("MSIE") != -1 && document.documentMode && document.documentMode == 8) {
            bg.style.top = "-231px";
        }
        quotetext.style.top = 0;
        //var letters = quoteLetterize();
        t = window.setTimeout(function () { typeQuote(t, index, 0/*, letters*/) }, 200)
    }
    else {
        quote.style.height = (1 + iteration) / 10 * (quotetext.offsetHeight - 2) + "px";
        quote.style.top = QUOTETOPBASE + (9 - iteration) / 10 * quotetext.offsetHeight / 2 + "px";
        if (navigator.userAgent.indexOf("MSIE") != -1 && document.documentMode && document.documentMode == 8 || IE) { quote.style.top = parseFloat(quote.style.top) - 6 + "px" }
        if (IE) { quote.style.top = parseFloat(quote.style.top) - 4 + "px" }
        window.setTimeout(function () { quoteExpand(t, index, iteration + 1), 200 })
    }
}

function quoteLetterize() {
    //alert('a')
    var str = quotetext.innerHTML;
    var res = [];
    if (quotetext.childNodes.length > 0) {
        if (quotetext.childNodes[0].nodeName.indexOf("text") != -1) {
            var cur = quotetext.childNodes[0];
            if (cur.data) { str = cur.data }
            else { alert('a') }
        }
        else { str = quotetext.childNodes[0].innerHTML; }
    }
    quotetext.innerHTML = "";
    str = str.split(" ");
    for (var i = 0; i < str.length; i++) {
        var wrd = str[i]
        var word = document.createElement("span");
        for (var j = 0; j < str[i].length; j++) {
            var letter = document.createElement("font");
            letter.className = "transparent";
            letter.innerHTML = wrd.substring(j, j + 1);
            word.appendChild(letter);
            res.push(letter)
        }
        quotetext.appendChild(word);
    }
    return res;
}

function typeQuote(t, index, iteration/*, array*/) {
    /*ShowState();*/
    /*if (iteration == array.length){
    window.clearTimeout(t)
    t = window.setTimeout(function(){
    quotetext.style.top = "-500px";
    slideOut(t, index, 0)
    },3000)
    }
    else{
    array[iteration].className = "visible";
    window.setTimeout(function(){typeQuote(t, index, iteration+1, array)},30)
    }*/
    if (iteration == 10) {
        window.clearTimeout(t);
        t = window.setTimeout(function () { unTypeQuote(t, index, 0) }, 4000)
    }
    else {
        quotetext.style.opacity = (iteration + 1) * 0.1;
        quotetext.style.filter = "alpha(opacity=" + (iteration + 1) * 10 + ")";
        t = window.setTimeout(function () { typeQuote(t, index, iteration + 1) }, 100)
    }
}

function unTypeQuote(t, index, iteration) {
    /*ShowState();*/
    if (iteration == 10) {
        window.clearTimeout(t);
        t = window.setTimeout(function () { quoteShrink(t, index, 0) }, 100)
    }
    else {
        quotetext.style.opacity = (9 - iteration) * 0.1;
        quotetext.style.filter = "alpha(opacity=" + (9 - iteration) * 10 + ")";
        t = window.setTimeout(function () { unTypeQuote(t, index, iteration + 1) }, 100)
    }
}

function quoteShrink(t, index, iteration) {
    /*ShowState();*/
    if (iteration == 10) {
        window.clearTimeout(t);
        t = window.setTimeout(function () { quoteSlideOut(t, index, 0) }, 10);
    }
    else {
        quote.style.height = (9 - iteration) / 10 * (quotetext.offsetHeight - 2) + "px";
        quote.style.top = QUOTETOPBASE + (1 + iteration) / 10 * quotetext.offsetHeight / 2 + "px";
        if (navigator.userAgent.indexOf("MSIE") != -1 && document.documentMode && document.documentMode == 8) { quote.style.top = parseFloat(quote.style.top) - 6 + "px" }
        window.setTimeout(function () { quoteShrink(t, index, iteration + 1), 200 })
    }
}

function quoteSlideOut(t, index, iteration) {
    /*ShowState();*/
    if (iteration == 10) {
        window.clearTimeout(t);
        t = window.setTimeout(function () { textSlideOut(t, index, 0) }, 300);
    }
    else {
        quote.style.left = ((1 + iteration) * (-356)) / 10 + "px";
        window.setTimeout(function () { quoteSlideOut(t, index, iteration + 1) }, 10);
    }
}

function textSlideOut(t, index, iteration) {
    /*ShowState();*/
    if (iteration == 10) {
        window.clearTimeout(t);
        processWidgetFeed(widgetfeed, index + 1)
    }
    else {
        low.style.left = ((1 + iteration) * (-356 / 10)) + "px";
        window.setTimeout(function () { textSlideOut(t, index, iteration + 1) }, 40);
    }
}

function initFaceWidget(source) {
    /*ShowState();*/
    var readyImages = [];
    document.getElementById("facewidget").style.visibility = "hidden";
    for (var i = 0; i < source.length; i++) {
        var temp = document.createElement("img");
        temp.src = source[i].img;
        temp.width = '356px';
        temp.height = '228px';
        temp.border = 0;
        temp.align = 'top';
        hider.appendChild(temp);
        readyImages.push(false);
        temp.i = i;
        temp.onload = function () { readyImages[this.i] = true; this.style.display = "none" };
    }
    var tt = window.setTimeout(
		function () {
		    bg.src = source[source.length - 1].img;
		    for (var j = 0; j < readyImages.length; j++) {
		        if (readyImages == false) { return }
		    }
		    window.clearTimeout(tt);
		    document.getElementById("facewidget").style.visibility = "";
		    processWidgetFeed(source, 0);
		},
		1000
	)
}



