// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function show_photo(id) {
    var parent_div = document.getElementById('auction_photos');
    for(var i = 0; i < parent_div.childNodes.length; i++) {
        var div = parent_div.childNodes[i];
        if('div' == div.nodeName || 'DIV' == div.nodeName) {
            div.style.display = 'none';
        }
    }
    var div1 = document.getElementById('auction_photo_' + id);
    div1.style.display = 'block';
}

function distributeResponse(response)
{
    var x = 0
    var auctionsData = response.split(":::")


    for (x=0;x<auctionsData.length-1;x++)
    {
 
        if (auctionsData[x].length > 4)
        {

            var auctionData = auctionsData[x].split(";")
            var auid = auctionData[0]

            if (document.getElementById('auction_'+auid+'m_timeleft') != null && auctionData.length > 3){
                document.getElementById('auction_'+auid+'m_currentprice').innerHTML = auctionData[1]+" z\u0142"
                document.getElementById('auction_'+auid+'m_currentleader').innerHTML = auctionData[2]
            }

            if (document.getElementById('auction_'+auid+'_timeleft') != null && auctionData.length > 3){
                document.getElementById('auction_'+auid+'_currentprice').innerHTML = auctionData[1]+" z\u0142"
                document.getElementById('auction_'+auid+'_currentleader').innerHTML = auctionData[2]
            }

            if (auctionData.length == 3)
                timeleft = parseInt(auctionData[1])
            else
                timeleft = parseInt(auctionData[3])


            if ((timeleft>=0 && auctionData.length > 3) || (timeleft <= 3600 && timeleft >= 0 && auctionData.length == 3))
            {
                hours = parseInt(timeleft/3600)
                minutes = parseInt((timeleft-(hours*3600))/60)
                seconds = timeleft-(hours*3600)-(minutes*60)

                if (document.getElementById('auction_'+auid+'m_timeleft') != null)
                    document.getElementById('auction_'+auid+'m_timeleft').innerHTML = ((hours < 10) ? "0" : "") + hours+":"+((minutes < 10) ? "0" : "") + minutes+":"+((seconds < 10) ? "0" : "") + seconds
                if (document.getElementById('auction_'+auid+'_timeleft') != null)
                    document.getElementById('auction_'+auid+'_timeleft').innerHTML = ((hours < 10) ? "0" : "") + hours+":"+((minutes < 10) ? "0" : "") + minutes+":"+((seconds < 10) ? "0" : "") + seconds
                
            }
            else if (timeleft == -3 && auctionData.length > 3){
                if (document.getElementById('auction_'+auid+'m_timeleft') != null)
                    document.getElementById('auction_'+auid+'m_timeleft').innerHTML = "Automaty"
                if (document.getElementById('auction_'+auid+'_timeleft') != null)
                    document.getElementById('auction_'+auid+'_timeleft').innerHTML = "Automaty"
            }
            else if (timeleft == -5){
                if (document.getElementById('auction_'+auid+'_timeleft') != null)
                    document.getElementById('auction_'+auid+'_timeleft').innerHTML = "Przerwa"
                if (document.getElementById('auction_'+auid+'m_timeleft') != null)
                    document.getElementById('auction_'+auid+'m_timeleft').innerHTML = "Przerwa"
            }

            else if (auctionData.length == 3 && timeleft <= 0){
                if (document.getElementById('price_'+auid) != null)
                    document.getElementById('price_'+auid).innerHTML = "Koniec"
                if (document.getElementById('price_'+auid) != null)
                    document.getElementById('price_'+auid).innerHTML = "Koniec"
                if (document.getElementById('auction_'+auid+'_timeleft') != null)
                    document.getElementById('auction_'+auid+'_timeleft').innerHTML = "&nbsp"
                if (document.getElementById('auction_'+auid+'m_timeleft') != null)
                    document.getElementById('auction_'+auid+'m_timeleft').innerHTML = "&nbsp"
            }

            else if (auctionData.length > 3){
                if (document.getElementById('auction_'+auid+'_timeleft') != null)
                    document.getElementById('auction_'+auid+'_timeleft').innerHTML = "Koniec"
                if (document.getElementById('auction_'+auid+'m_timeleft') != null)
                    document.getElementById('auction_'+auid+'m_timeleft').innerHTML = "Koniec"
            }
            
            if (timeleft<5) {
                setClassName(document.getElementById('auction_'+auid+'_timeleft'),"card_timer_red");
                setClassName(document.getElementById('auction_'+auid+'m_timeleft'),"card_timer_red")
            } else {
                setClassName(document.getElementById('auction_'+auid+'_timeleft'),"card_timer");
                setClassName(document.getElementById('auction_'+auid+'m_timeleft'),"card_timer");
            }
            //ilość bijących sie automatów
            try {
                if (timeleft < 20 && auctionData.length > 3){
                    document.getElementById('auto_'+auid).innerHTML = auctionData[4]
                }else{
                    document.getElementById('auto_'+auid).innerHTML = ""
                }
            } catch(e) {
                
            }

        }

    }
//document.getElementById('user_currentpoints').innerHTML = response.slice(response.lastIndexOf('pts:')+4)
}

function details(response,id)
{
    watch_price(document.getElementById('auction_'+id+'_currentprice').innerHTML,id)
    distributeResponse(response)
      

    document.getElementById('aktualna').innerHTML = document.getElementById('auction_'+id+'_currentprice').innerHTML
    //alert(parseFloat(document.getElementById('sklepowa').innerHTML) - parseFloat(document.getElementById('aktualna').innerHTML))
    a = parseFloat(document.getElementById('sklepowa').innerHTML)
    b = parseFloat(document.getElementById('aktualna').innerHTML)
    zysk = a - b
    if (zysk < 0)
        document.getElementById('zysk').innerHTML = " 0.00 zł"
    else
        document.getElementById('zysk').innerHTML = zysk.toFixed(2) +" zł"
}

function setClassName(obj,classname)
{
    try {
        obj.setAttribute("class", classname);
        obj.setAttribute("className", classname);
    } catch(e) {

    }
}


//function updateBingujButton(auction_id, response) {
//
//    var button_orig = byID('binguj_' + auction_id + '_button');
//
//    switch(response) {
//        case 't':
//            setClassName(button_orig,"binguj_button_greener");
//            break;
//        default:
//            setClassName(button_orig,"binguj_button_red");
//            break;
//    }
//    setTimeout(function(){setClassName(button_orig,"binguj_button");}, 250);

//    return;
//}

function updateBingujButton(auction_id, response) {

    var img_orig = $('binguj_' + auction_id);
    var src_orig = img_orig.src
    var img;
    switch(response) {
        case 't':
            img = $('binguj_button_green');
            break;
        default:
            img = $('binguj_button_red');
            break;
    }


    setTimeout(function(){
        img_orig.src = img.src;
    }, 10);
    setTimeout(function(){
        img_orig.src = src_orig;
    }, 500);

    return;
}

function timer_for_buy_now(au_id){

    var start = parseInt(document.getElementById('timer_'+au_id).innerHTML);
    start--
    document.getElementById('timer_'+au_id).innerHTML = start

    if (start <= 0){

        new Ajax.Updater('auction_'+au_id+'_buy_now', '/auction/check_price/'+au_id+'?end_time=true',
        {
            asynchronous:true,
            evalScripts:true
        });
        clearInterval(getCookie(au_id));
        deleteCookie(au_id)
        return;
    }


}

function setCookie(id, value, expire) {
    document.cookie = id + "=" + escape(value) + ((expire==null)?"" : ("; expires=" + expire.toGMTString()))
}

function getCookie(id) {
    if (document.cookie!="") {

        var toCookie=document.cookie.split("; ");
        
        for (i=0; i<toCookie.length; i++) {
            var idCookie=toCookie[i].split("=")[0];
            var valueCookie=toCookie[i].split("=")[1];
            if (idCookie==id) return unescape(valueCookie)
        }
    }
}

function deleteCookie(id) {
    var dzis = new Date()

    if (document.cookie!="") {

        var toCookie=document.cookie.split("; ");
   
        for (i=0; i<toCookie.length; i++) {
            var nazwaCookie=toCookie[i].split("=")[0];
            if (nazwaCookie==id)
                document.cookie=nazwaCookie+"=;expires="+dzis.getMonth()-1
        }
    }
}

function new_buy_now_auction(){
    if (document.getElementById("auction_buy_now").checked){

        document.getElementById("auction_is_autobidder_allowed").style.visibility = 'hidden';
        document.getElementById("auction_is_nighttime").style.visibility = 'hidden';
        document.getElementById("auction_is_for_beginners_only").disabled = true
        document.getElementById("auction_is_fixed_price").disabled = true
        document.getElementById("auction_auction_return_point_attributes_is_return_points").disabled = true
        document.getElementById("auction_trade_for_points").disabled = true
        document.getElementById("auction_points_required").disabled = true;
        document.getElementById("auction_auction_return_point_attributes_first_leader_return_points").disabled = true
        document.getElementById("auction_auction_return_point_attributes_second_leader_return_points").disabled = true


        document.getElementById("auction_is_autobidder_allowed").checked = 'checked';
        document.getElementById("auction_is_nighttime").checked = 'checked';
        document.getElementById("auction_is_for_beginners_only").checked = false;
        document.getElementById("auction_is_fixed_price").checked = false;
        document.getElementById("auction_auction_return_point_attributes_is_return_points").checked = false;
        document.getElementById("auction_trade_for_points").value = '';
        document.getElementById("auction_points_required").value = '';
        document.getElementById("auction_auction_return_point_attributes_first_leader_return_points").value = '0';
        document.getElementById("auction_auction_return_point_attributes_second_leader_return_points").value = '0';


    }else{
        document.getElementById("auction_is_autobidder_allowed").style.visibility = 'visible';
        document.getElementById("auction_is_nighttime").style.visibility = 'visible';
        document.getElementById("auction_is_for_beginners_only").disabled = false
        document.getElementById("auction_is_fixed_price").disabled = false
        document.getElementById("auction_auction_return_point_attributes_is_return_points").disabled = false
        document.getElementById("auction_trade_for_points").disabled = false
        document.getElementById("auction_points_required").disabled = false
        document.getElementById("auction_auction_return_point_attributes_first_leader_return_points").disabled = false
        document.getElementById("auction_auction_return_point_attributes_second_leader_return_points").disabled = false
    }

}
