/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/             
 
var Url = {     
 
    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },
 
    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },
 
    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";
 
        for (var n = 0; n < string.length; n++) {
 
            var c = string.charCodeAt(n);
 
            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
 
        }
 
        return utftext;
    },
 
    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
 
        while ( i < utftext.length ) {
 
            c = utftext.charCodeAt(i);
 
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
 
        }
 
        return string;
    }
 
}; 


var DDMenu = { 
     
    nTimer: null,

    nDelay: 500,
    pLastItem: null,
    pCurrnet: null, 
    pLastParent: null,
    
    init: function(strBlockClass) 
    {
        var nCount = 0;
        $("div."+strBlockClass).children().each(function() 
        {
            if($(this).is("a")) 
            {
                nCount++;
                $(this).attr('id', 'menu'+nCount); 
                // if($(this).attr('class').indexOf('current') != -1) { DDMenu.pCurrnet = '#' + $(this).attr('id'); }
                $(this).mouseover(function() { $(this).animate({'paddingLeft':15}, 100) });
                $(this).mouseout(function() { $(this).animate({'paddingLeft':10}, 100) });
                
                if($(this).hasClass('menucurrent')) 
                {
                    DDMenu.pLastItem = '#sub' + $(this).attr('id');
                    DDMenu.pLastParent = '#'+$(this).attr('id');
                }
                
                $(this).click(function() {
                    if('#'+$(this).attr('id') == DDMenu.pLastParent)
                    {
                        DDMenu.pLastItem = null; 
                        DDMenu.pLastParent = null;
                        var strSubID = '#sub' + $(this).attr('id');
                        DDMenu.hideSubmenu(strSubID, $(strSubID+" li").length);
                        $(this).attr('class', 'menuparent');
                    }
                    else
                    {
                        var strSubID = '#sub' + $(this).attr('id');
                        if($(strSubID).length)
                        { 
                            if(DDMenu.pLastItem)
                            {
                                $(DDMenu.pLastParent).attr('class', 'menuparent'); 
                                DDMenu.hideSubmenu(DDMenu.pLastItem, $(DDMenu.pLastItem+" li").length); 
                            }
                            DDMenu.pLastItem = strSubID; 
                            DDMenu.pLastParent = '#' + $(this).attr('id'); 
                            
                            $(this).attr('class', 'menucurrent');
                            
                            $(strSubID).css({display: 'block'});
                            DDMenu.displaySubmenu(strSubID, 0);
                        }
                    }
                });

            } 
            if ($(this).is('ul')) 
            {
                $(this).attr('id', 'submenu'+nCount);
                $(this).find('li a').each(function()
                {
                    $(this).mouseover(function() { $(this).animate({'paddingLeft':25}, 100) });
                    $(this).mouseout(function() { $(this).animate({'paddingLeft':20}, 100) });
                });
            }  
        });
    },
    
    displaySubmenu: function(strMenuID, nItem)
    {
        var pItem = $(strMenuID+" li:eq("+nItem+")");
        if(pItem.length)
        {
            pItem.css({display: 'block', opacity:0});
            pItem.animate({opacity:1}, 50, function() { DDMenu.displaySubmenu(strMenuID, nItem+1); } );
        }
    }, 
    
    hideSubmenu: function(strMenuID, nItem)
    {
        nItem--;
        var pItem = $(strMenuID+" li:eq("+nItem+")");
        if(pItem.length)
        {
            pItem.css({display: 'none', opacity:0});
            pItem.animate({opacity:0}, 50, function() { DDMenu.hideSubmenu(strMenuID, nItem); } );
        }
    }
    
};

var Promo = {
    pTimer: null,
    pCurrent: null,
    bPlay: true,
    
    init: function() 
    {
        var nCount = 0;
        if($("div.promobox").length)
        {
            //Set the opacity of all images to 0         
            $("div.promobox div").each(function() 
            { 
                $(this).css({opacity: 0.0, position: 'absolute', display:'none'});
                nCount++;
            });
            
            //Get the first image and display it (gets set to full opacity)
            $('div.promobox div:first').css({opacity: 1.0, display:'block'}); 
        }
        else
        {
            Promo.bPlay = false;
        }
        if(nCount < 2)
        {
            Promo.bPlay = false;
        }
        
        $("div#promos div").each( function(nID) 
        { 
            $(this).click(function() { if(Promo.bPlay) { Promo.rotate(nID+1); } });
            $(this).mouseover(function() { if($(this).hasClass('promo')) { $(this).find('img').css({'opacity':0.7}); } });
            $(this).mouseout(function() { if($(this).hasClass('promo')) { $(this).find('img').css({'opacity':0.5}); } }); 
        });
        
        if(Promo.bPlay)
        {
            this.pTimer = setTimeout('Promo.rotate()', 5000);
        }
    },
    
    rotate: function (nNext) 
    {           
        if(!Promo.bPlay)
        {
            return false;
        }      
         
        Promo.bPlay = false;  
          
        if (this.pTimer) 
        { 
            clearTimeout(this.pTimer); 
        }
        if(this.pCurrent)
        {
            this.pCurrent.css({'z-index':'0', display:'block', opacity: 0.0});    
        }  
                              
        //Get the first image
        var current = ($('div.promobox div.show').length ? $('div.promobox div.show') : $('div.promobox div:first'));  
        var currentLink = $('div#promos div.currnet');      
        this.pCurrent = current;
        
        //Get next image, when it reaches the end, rotate it back to the first image
        if(nNext)
        {
            nNext -= 1;
            var next = $('div.promobox div:eq('+nNext+')');
            var nextLink = $('div#promos div:eq('+nNext+')');  
        }
        else
        {
            var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.promobox div:first') :current.next()) : $('div.promobox div:first'));    
            var nextLink = currentLink.next().length ? currentLink.next() : $('div#promos div:first'); 
        }
        
        current.removeClass('show').css({'z-index':'1'});
        //current.removeClass('show').css({'z-index':'1', display:'none'});
        
        //Set the fade in effect for the next image, the show class has higher z-index
        next.css({opacity: 0.0}).addClass('show').css({'z-index':'2', display:'block'}).animate({opacity: 1.0}, 500, function() { Promo.bPlay = true; });
        currentLink.attr('class', 'promo'); 
        currentLink.find('img').css({'opacity': 0.5}); 
        nextLink.attr('class', 'currnet'); 
        nextLink.find('img').css({'opacity': 1});
        
        this.pTimer = setTimeout('Promo.rotate()', 5000); 
    }
};

$(document).ready(function() { 
    DDMenu.init('menu');   
    Promo.init();
    
    var strBannersBlocURL = $('#bannerblock').html();
    $('#bannerblock').html('');
    if(strBannersBlocURL != '')
    {
        $.ajax({
            cache: false,
            url: Url.encode(strBannersBlocURL),
            success: function(data) {
                if(data != '')
                { 
                    $('#bannerblock').html(data);
                    $('#bannerblock').css({'display':'block'});
                    if($('.editableframe').length)
                    {
                        Admin.init();
                    }
                }
            }
        });
    }
});

function showTown(strTown)
{
    //alert(strTown); 
    $('.objects').children().each(function() 
    {
        if($(this).is("div")) 
        {
            $(this).css({display:'none'});    
        }
    }); 
    $('.objects #'+strTown).css({display:'block'});                                       
} 

var Inquiry = {
    vote: function(strTargetURL) 
    {
        // alert(strTargetURL);
        var strRadioVal = $("#inquiry input:radio:checked").val();
        if(strRadioVal)
        {
            var params = 'select='+strRadioVal;
            $.ajax({
                cache: false,
                type: "POST",
                data: params,
                url: Url.encode(strTargetURL),        
                success: function(data) {
                    //alert(data); 
                    $("#inquiry").replaceWith(data);  
                }
            }); 
        }
        else
        {
            if(strLanguage == 'bg')
            {
                alert('Моля, посочете стойност');
            }
            else
            {
                alert('Please select your vote');
            }
        }       
    }
} 


var EShop = {
    login: function()
    {
        location.href = 'http://elitcar.innovasys-bg.com/eshop/login.aspx?username='+$('.hright input#username').val()+'&password='+$('.hright input#password').val()+'&lang='+$('.hright input#lang').val(); 
    }
}

