﻿var next = 2;
var previous = 3;
var current = 1;
var thedate = new Date();
var NumOfArticles;
var extraQueryStringMainArticle = "";

var rndNum = thedate.getHours() + 'a' + thedate.getDay();
var ArticelIntervalId = 0;
function openItem(id, realID, incNum) {
    on = id;
    clearTimeout(openProd);
    openProd = setTimeout(function() {
        if (on == id) {

            if (id != nowOpen) {
                // $('promoMain').className ='promo_tabs opened';
                if (nowOpen > 0) {
                    if (nowOpen == 5)
                        $('promo5').className = 'last';
                    else
                        $('promo' + nowOpen).className = '';
                }
                else {
                    if (id != 1)
                        $('promo1').className = '';
                }

                $('promo' + id).className = 'current';

                var fade = $('MainArticle');
                fade.setOpacity(0);
                var url = "/ajax/mainarticle.aspx?id=" + realID + "&p=" + rndNum + "&c=" + incNum + extraQueryStringMainArticle;
                new Ajax(url, {
                    method: 'get',
                    update: $('MainArticle'), evalScripts: true,
                    onComplete: function() {
                        var fx = new Fx.Style(fade, 'opacity', { duration: 400 });
                        fx.start(0, 1).chain(function() {
                            isOut = false;
                        })
                    }
                }).request();
                nowOpen = id;
            }
        }
    }, 130);
}


//openItem(id, realID, incNum);
//var currentMainArticle = 1;
//var nextMainArticle = 2;
//function setNextMainArticle(id) {
//    var NumOfMainArticles = 5;
//    if (id < NumOfMainArticles + 1) {
//        if (id == NumOfMainArticles) {
//            currentMainArticle = id;
//            nextMainArticle = 1;
//        }
//        else if (id == 1) {
//            currentMainArticle = id;
//            nextMainArticle = 2;
//        }
//        else {
//            currentMainArticle = id;
//            nextMainArticle = id + 1;
//        }
//    }
//    else {
//        currentMainArticle = NumOfMainArticles - 1;
//        nextMainArticle = 1;
//    }
//}

function OpenNextMainArticle() {
    if (nowOpen == 5) {
        openItem(1, mainArticles[0]);
        }
        else {
            openItem(nowOpen + 1, mainArticles[nowOpen]);
    }
}



function openVoucherItem(id, realID, incNum) {
    on = id;
    clearTimeout(openProd);
    openProd = setTimeout(function() {
        if (on == id) {

            if (id != nowOpen) {
                // $('promoMain').className ='promo_tabs opened';
                if (nowOpen > 0) {
                    if (nowOpen == 5)
                        $('promo5').className = 'last';
                    else
                        $('promo' + nowOpen).className = '';
                }
                else {
                    if (id != 1)
                        $('promo1').className = '';
                }

                $('promo' + id).className = 'current';

                var fade = $('MainRestsVouchers');
                fade.setOpacity(0);
                var url = "/ajax/RestsVouchers.aspx?RegionID=" + realID + "&c=" + incNum; //"&p=" + rndNum
                new Ajax(url, {
                    method: 'get',
                    update: $('MainRestsVouchers'), evalScripts: true,
                    onComplete: function() {
                        var fx = new Fx.Style(fade, 'opacity', { duration: 400 });
                        fx.start(0, 1).chain(function() {
                            isOut = false;
                        })
                    }
                }).request();
                nowOpen = id;
            }
        }
    }, 130);
}

function closeVoucherItem(id) {
    on = 0;
    clearTimeout(closeProd);
    closeProd = setTimeout(function() {
    }, 0); // Set the timeout value here. I used 1 second for testing. 
}

function closeItem(id) {
    on = 0;
    clearTimeout(closeProd);
    closeProd = setTimeout(function() {
    }, 0); // Set the timeout value here. I used 1 second for testing. 
}

function showAjax(id) {
    if (id > NumOfArticles || id < 1)
        i = (id % NumOfArticles);
    else
        i = id;
    if (i == -1) i = NumOfArticles;
    for (j = 1; j <= NumOfArticles; j++)
        $('img_' + j).src = "/images/butt_red_empty.gif";
    $('img_' + i).src = "/images/butt_red_full.gif";
    var fade = $('product');
    fade.setOpacity(0);
    var url = "/ajax/MainPromo.aspx?id=" + id + "&p=" + rndNum;
    new Ajax(url, {
        method: 'get',
        update: $('product'),
        onComplete: function() {
            var fx = new Fx.Style(fade, 'opacity', { duration: 500, wait: false });
            fade.setStyle('display', 'block');
            fx.start(0, 1);
        }
    }).request();
}

function setNextPrevLinks(id) 
{
    if (id < 4) {
        next = id + 1;
        if (id == 1) {
            previous = id;
        }
        previous = id - 1;
    }
    else {
        next = id;
        previous = id - 1;
    }

}

function setNext(id) {
    if (id < NumOfArticles + 1)
     {
        if (id == NumOfArticles) {
            current = id;
            previous = NumOfArticles - 1;
            next = 1;
        }
        else if (id == 1) {
            current = id;
            previous = NumOfArticles;
            next = 2;
        }
        else {
           current = id;
            previous = id - 1;
            next = id + 1;
        }
    }
    else {
        current = NumOfArticles - 1;
        previous = NumOfArticles - 2;
        next = 1;
    }
}

function setPrevious(id) {

    if (id > 0) {
        if (id == 1) {
            current = id;
            previous = NumOfArticles;
            next = 2;
        }
        else
            if (id == NumOfArticles) 
            {
                current = NumOfArticles;
                previous = NumOfArticles - 1;
                next = 1;
        }
        else {
            current = id;
            previous = id - 1;
            next = id + 1;
        }

    }
}

function RestartArticlesTimer() {
    clearInterval(ArticelIntervalId);
    initArticlesTimer(8000);
}


function initArticlesTimer(time) {
    ArticelIntervalId = setInterval('setNext(next);showAjax(current); OpenNextMainArticle();', time); //noni
}


function initExtraQueryStringMainArticle(extraQuery) {
    extraQueryStringMainArticle = extraQuery;
}


function ShowCurrentArticle(id) {

    if (id > NumOfArticles || id < 1)
        i = (id % NumOfArticles);
    else
        i = id;
    if (i == -1) i = NumOfArticles;
    for (j = 1; j <= NumOfArticles; j++)
        $('img_' + j).src = "/images/butt_red_empty.gif";
    $('img_' + i).src = "/images/butt_red_full.gif";

    if (id == 1) {
        ChangeArticlesVisibility('uc_Article1_rp_Articles_ctl00_AnchorArticle1', 'uc_Article1_rp_Articles_ctl01_AnchorArticle2', 'uc_Article1_rp_Articles_ctl02_AnchorArticle3');
        ChangeArticlesVisibility('uc_Article1_rp_Articles_ctl00_divArticle1', 'uc_Article1_rp_Articles_ctl01_divArticle2', 'uc_Article1_rp_Articles_ctl02_divArticle3');
        
    }
    else if (id == 2) {
    ChangeArticlesVisibility('uc_Article1_rp_Articles_ctl01_AnchorArticle2', 'uc_Article1_rp_Articles_ctl00_AnchorArticle1', 'uc_Article1_rp_Articles_ctl02_AnchorArticle3');
    ChangeArticlesVisibility('uc_Article1_rp_Articles_ctl01_divArticle2', 'uc_Article1_rp_Articles_ctl00_divArticle1', 'uc_Article1_rp_Articles_ctl02_divArticle3');
    
    }
    else if (id == 3) {
    ChangeArticlesVisibility('uc_Article1_rp_Articles_ctl02_AnchorArticle3', 'uc_Article1_rp_Articles_ctl00_AnchorArticle1', 'uc_Article1_rp_Articles_ctl01_AnchorArticle2');
    ChangeArticlesVisibility('uc_Article1_rp_Articles_ctl02_divArticle3', 'uc_Article1_rp_Articles_ctl00_divArticle1', 'uc_Article1_rp_Articles_ctl01_divArticle2');

    }
}

function ChangeArticlesVisibility(visible1, notVisible1, notVisible2) {
    var cur1 = document.getElementById(visible1);
    cur1.style.display = 'block';

    var cur2 = document.getElementById(notVisible1);
    cur2.style.display = 'none';

    var cur3 = document.getElementById(notVisible2);
    cur3.style.display = 'none';
    
}

function changeUp(divName, downImgId, upImgId) {
    document.getElementById(divName).style.height = '50px';
    document.getElementById(divName).style.bottom = '73px';
    document.getElementById(downImgId).style.display = 'block'
    document.getElementById(upImgId).style.display = 'none';
    clearInterval(ArticelIntervalId);
}
function changeDown(divName, downImgId, upImgId) {
    document.getElementById(divName).style.height = '10px';
    document.getElementById(divName).style.bottom = '32px';
    document.getElementById(upImgId).style.display = 'block'
    document.getElementById(downImgId).style.display = 'none';
    initArticlesTimer(8000);
}

function updateCouponCategory(newID, dropDownClientID, newName) {
            document.getElementById(dropDownClientID).innerHTML = newName;
        showCouponAjax(newID);
    return false;
}

function showCouponAjax(regionID) {
    var fade = $('div_CouponDetails');
    fade.setOpacity(0);
    var url = "/ajax/CouponDetails.aspx?RegionID=" + regionID + "&p=" + rndNum;
    new Ajax(url, {
        method: 'get',
        update: $('div_CouponDetails'),
        onComplete: function() {
            var fx = new Fx.Style(fade, 'opacity', { duration: 500, wait: false });
            fade.setStyle('display', 'block');
            fx.start(0, 1);
        }
    }).request();
}

function showGroupVoucherAjax(dropDownClientID, selectedDDLid) {
    $(dropDownClientID).innerHTML = $(selectedDDLid).innerHTML;
    var fade = $('div_CouponDetails');
    fade.setOpacity(0);
    var url = "/ajax/GroupVoucherDetails.aspx";
    new Ajax(url, {
        method: 'get',
        update: $('div_CouponDetails'),
        onComplete: function() {
            var fx = new Fx.Style(fade, 'opacity', { duration: 500, wait: false });
            fade.setStyle('display', 'block');
            fx.start(0, 1);
        }
    }).request();
}


function PlayVideoLobby(id, realID, numVideos) {
    var i;
    for (i = 1; i <= numVideos; i++) {

        if (i == id) {
            $('promo' + i).className = 'current';
        }
        else if (i == numVideos) {
            $('promo' + i).className = 'last';
        }
        else {
            $('promo' + i).className = '';
        }
    }

}




function showhideCouponDropDown(layer_ref) {
    var dis = 'none';
    if (document.getElementById(layer_ref).style.display == 'block') {
        document.getElementById(layer_ref).style.display = 'none';
        bringToFront(layer_ref);
        
    }
    else {
        document.getElementById(layer_ref).style.display = 'block';
        bringToFront(layer_ref);
    }

 
}




function getAbsoluteDivs() {
    var arr = new Array();
    var all_divs = document.body.getElementsByTagName("DIV");
    var j = 0;

    for (i = 0; i < all_divs.length; i++)
        if (all_divs.item(i).style.position == 'absolute') {
        arr[j] = all_divs.item(i);
        j++;
    }

    return arr;
}

function bringToFront(id) {
    if (!document.getElementById ||
        !document.getElementsByTagName)
        return;

    var obj = document.getElementById(id);
    var divs = getAbsoluteDivs();
    var max_index = 0;
    var cur_index;

    // Compute the maximal z-index of  
    // other absolute-positioned divs  
    for (i = 0; i < divs.length; i++) {
        var item = divs[i];
        if (item == obj ||
            item.style.zIndex == '')
            continue;

        cur_index = parseInt(item.style.zIndex);
        if (max_index < cur_index) {
            max_index = cur_index;
        }
    }

    obj.style.zIndex = max_index + 1;
}

function sendToBack(id) {
    if (!document.getElementById ||
        !document.getElementsByTagName)
        return;

    var obj = document.getElementById(id);
    var divs = getAbsoluteDivs();
    var min_index = 999999;
    var cur_index;

    if (divs.length < 2)
        return;

    // Compute the minimal z-index of  
    // other absolute-positioned divs  
    for (i = 0; i < divs.length; i++) {
        var item = divs[i];
        if (item == obj)
            continue;

        if (item.style.zIndex == '') {
            min_index = 0;
            break;
        }

        cur_index = parseInt(item.style.zIndex);
        if (min_index > cur_index) {
            min_index = cur_index;
        }

    }

    if (min_index > parseInt(obj.style.zIndex)) {
        return;
    }

    obj.style.zIndex = 1;

    if (min_index > 1)
        return;

    var add = min_index == 0 ? 2 : 1;

    for (i = 0; i < divs.length; i++) {
        var item = divs[i];
        if (item == obj)
            continue;

        item.style.zIndex += add;
    }
}


function countDown(hrs1, min1, sec1) {
    sec--;
    if (sec == -01) {
        sec = 59;
        min--;
        if (min == -01) {
            min = 59;
            hrs--;
        }
    } else {
        min = min;
    }
    if (sec <= 9) { sec = "0" + sec; }
    document.getElementById("time_seconds").innerHTML = sec;
    document.getElementById("time_minutes").innerHTML = (min <= 9 ? "0" + min : min);
    document.getElementById("time_houres").innerHTML = (hrs <= 9 ? "0" + hrs : hrs);
    SD = window.setTimeout("countDown();", 1000);
    if (hrs == '00' && min == '00' && sec == '00') { sec = "00"; window.clearTimeout(SD); }
}

function getElementsByClassName(node, classname) {
    if (node.getElementsByClassName) { // use native implementation if available
        return node.getElementsByClassName(classname);
    } else {
        return (function getElementsByClass(searchClass, node) {
            if (node == null)
                node = document;
            var classElements = [],
            els = node.getElementsByTagName("*"),
            elsLen = els.length,
            pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)"), i, j;

            for (i = 0, j = 0; i < elsLen; i++) {
                if (pattern.test(els[i].className)) {
                    classElements[j] = els[i];
                    j++;
                }
            }
            return classElements;
        })(classname, node);
    }
}

function changeImage(obj) {
    if (obj.getProperty('class') == 'item current')
        return;
    var curNum = $(getElementsByClassName(document.getElementById('uc_HomePageBox_uc_Images_pnl_numbers'), 'item current')[0]);
    curNum.setProperty('class', 'item');

    obj.setProperty('class', 'item current');
    var dealImage = $('uc_HomePageBox_uc_Images_img_dealImage');
    dealImage.setProperty('src', obj.getProperty('href'));
    

}

function prevImage() {
    var curr = $(getElementsByClassName(document.getElementById('uc_HomePageBox_uc_Images_pnl_numbers'), 'item current')[0]);
    curr = curr.getPrevious();
    if (curr.innerHTML != '&nbsp;')
        changeImage(curr);
}

function nextImage() {
    var curr = $(getElementsByClassName(document.getElementById('uc_HomePageBox_uc_Images_pnl_numbers'), 'item current')[0]);
    curr = curr.getNext();
    if (curr.innerHTML != '&nbsp;')
        changeImage(curr);
}
