var scrollParams = {
    scrollbarWidth:14, 
    showArrows:false, 
    wheelSpeed:100,
    verticalGutter:3,
    verticalDragMinHeight:48
};

$().ready(function() {
    var ResultSelectedDate;
    	
    /********************* Tabs **********************/
    $('div.tabs > ul.tabnames > li').click(function() {
        var $names = $(this).parent(), $tabs = $names.parent().find('> div');
        $tabs.find('> div.active').removeClass('active');
        $names.find('> li.active').removeClass('active');
        $tabs.find('> div:eq('+$(this).index()+')').addClass('active');
        $(this).addClass('active');
    });
	
    /*************** Select processing ***************/
    //    $('select.selectbox').jListbox();	
    //	
    //    $('div.datepicker').datePicker({
    //        inline:true, 
    //        renderCallback: function($td, date) {
    //            if ($td.is('.other-month')) return false;		
    //            if (window.AR_DATES && typeof AR_DATES == 'object') {
    //                var d = date.getDate(), m = date.getMonth() + 1, y = date.getFullYear(),
    //                patr = new RegExp((d < 10 ? '0' + d : d) + '\\D' + (m < 10 ? '0' + m : m) + '\\D' + y);
    //                for (var i in AR_DATES) {
    //                    if (patr.test(i)) $td.addClass('color' + AR_DATES[i]);
    //                }
    //            }
    //        }
    //    }).bind('dpMonthChanged', function() {
    //        $('tr:nth-child(odd)', this).addClass('odd');
    //    }).bind('dateSelected',function(e, selectedDate, $td){
    //        ResultSelectedDate = selectedDate;
    //    });
    //	
    //    $('div.datepicker tr:nth-child(odd)').addClass('odd');
	
    /******************* Accordion *******************/
    $('ul.accordionctrl li').live('click', function() {
        var $this = $(this), $ul = $this.parent(), $div = $('div.accordion');		
        $div.find('> div:eq('+$this.index()+') h2.caption').trigger('click', [true]);
        if ($('div.accordion').is('.extend')) {
            $ul.find('> li.uncollapse').not(this).removeClass('uncollapse');
        }
    });
	
    $('div.accordion:not(.extend) h2.caption').live('click', function(event, dontApply) {
        var $parent = $(this).parent(), $ul = $('ul.accordionctrl');
        $parent.find('div.itemdata').slideToggle('fast');
        if (!dontApply) {
            $ul.find('> li:eq('+$parent.index()+')').toggleClass('uncollapse');
        }
        $parent.toggleClass('uncollapse');
    });
    
    var clicked = false;
    
    $('div.extend h2.caption').live('click', function(event, dontApply) {
        var $parent = $(this).parent(), $ul = $('ul.accordionctrl'), $active;
        //        $parent.find('div.itemdata').slideToggle('fast');
        
        $parent.parent().find('div.uncollapse > div.itemdata').slideUp('fast', function() {
            $(this).parent().removeClass('uncollapse');
        });
        
        if(!$parent.hasClass('uncollapse')){
            $parent.addClass('uncollapse');
            $('.itemdata',$parent).slideDown('fast');
        }

        if ($ul.length) {
            $active = $ul.find('> li:eq('+$parent.index()+')');
            $ul.find('> li.uncollapse').not($active.get(0)).removeClass('uncollapse');
            $active.toggleClass('uncollapse');
        }
    });
    
    $('.complexaccordion h2.caption').live('click', function() {
        if(!$(this).is('.noclick')){
            var $parent = $(this).parent().parent().parent();
            var itemdata = $parent.find('div.itemdata');
            if(itemdata.is(':visible')){
                itemdata.css('display','none');
            }
            else{
                itemdata.css('display','block');
            }
            $parent.toggleClass('uncollapse');
        }
    });
	
    $('#allcollapse').click(function() {
        var text = 'бонусы', collapse = 'Свернуть все ', uncollapse = 'Развернуть все ';
        if ($(this).is('.siterules')) text = 'Правила';
        if ($(this).is('.news')) {
            text = 'новости';
            collapse = 'Скрыть все ';
            uncollapse = 'Показать все ';
        }
        if($(this).is('.linecollapse')){
            
            if ($(this).is('.uncollapse')) {
                $(this).find('span').text('Свернуть все');
                $(this).removeClass('uncollapse');
                $('#line #actions .champs div.item').addClass('uncollapse').find('div.itemdata').show();
            }
            else {
                $(this).find('span').text('Развернуть все');
                $(this).addClass('uncollapse');
                $('#line #actions .champs div.item').removeClass('uncollapse').find('div.itemdata').hide();
            }
            return false;
        }
        if (this.collapseClick) {
            $(this).find('span').text(uncollapse + text);
            $('ul.accordionctrl li.uncollapse').removeClass('uncollapse');
            $('div.accordion div.itemdata').slideUp('fast').parent().removeClass('uncollapse');
            this.collapseClick = false;
        }
        else {
            $(this).find('span').text(collapse + text);
            $('ul.accordionctrl li').addClass('uncollapse');
            $('div.accordion div.itemdata').slideDown('fast').parent().addClass('uncollapse');
            this.collapseClick = true;
        }
        $(this).toggleClass('uncollapse');
    });
	
    $('#hideratesample').click(function() {
        if (this.hideClick) {
            $(this).text('Скрыть пример ставки и список призов');
            $('#ratesample').slideDown('fast');
            this.hideClick = false;
        }
        else {
            $(this).text('Показать пример ставки и список призов');
            $('#ratesample').slideUp('fast');
            this.hideClick = true;
        }
    });
	
    $('#hidedetails').click(function() {
        if (this.hideClick) {
            $(this).text('Скрыть подробности');
            $('#partnerdetails').slideDown('fast');
            this.hideClick = false;
        }
        else {
            $(this).text('Показать подробности');
            $('#partnerdetails').slideUp('fast');
            this.hideClick = true;
        }
    });
	
    $('#printrules').click(function() {
        $('body').addClass('printsiterules');
        window.print();
        if ($.browser.opera) {
            $('html').toggleClass('operaUpdate');
        }
    });
	
    $('input[type=radio],input[type=checkbox]').each(function() {
        var $tag = $(this).next();
        if ($(this).custCheckBox) {
            $(this).custCheckBox();
            if ($tag.is('label')) {
                $tag.click(function() {
                    $(this).prev().prev().trigger('click');
                });
                $tag.hover(function(){
                    $(this).prev().prev().addClass('hvr');
                },
                function(){
                    $(this).prev().prev().removeClass('hvr');
                });
            }
        }
    });
	
    btnProcessing();
	
    $('#actions.lines h2.caption span.cust_checkbox').click(function(event) {
        var $next = $(this).parent().next();
        event.stopPropagation();
        if ($(this).is('.cust_checkbox_on')) {
            $next.find('span.cust_checkbox').removeClass('cust_checkbox_off').addClass('cust_checkbox_on');
        }
        else {
            $next.find('span.cust_checkbox').removeClass('cust_checkbox_on').addClass('cust_checkbox_off');
        }
    });
    
    
    
    $('#removeflash').click(function() {
        var $flash = $('#headflash'), $span = $('span span', this), 
        expires = new Date(new Date()*1 + 24*60*60*1000);
        if ($flash.is(':visible')) {
            $('#headflash').hide();
            SetCookie('HeadFlash_Show', 'N', expires, '/');
            $('#logocopy').css({
                position:'relative', 
                zIndex:1, 
                zoom:1
            });
            $span.text('Показать Flash');
        }
        else {
            $('#logocopy').css({
                position:'absolute', 
                zIndex:0
            });
            $('#headflash').show();
            SetCookie('HeadFlash_Show', 'Y', expires, '/');
            $span.text('Убрать Flash');
        }
    });
    
    droplistProcessing();
});

function showLoading() {
    var container = document.documentElement, 
    top = container.scrollTop, height = container.clientHeight;
    if ($.browser.msie) {
        container = document.body;
        top = document.documentElement.scrollTop
        height = document.documentElement.clientHeight;
    }
    $('<div id="windowOverlay"><div id="processing">&nbsp;</div></div>').appendTo('body').height(container.scrollHeight);
    $('#processing').css({
        height:height, 
        top:top
    });
}
function hideLoading() {
    $('#windowOverlay:has(#processing)').remove();
}

function GetCookie(name) {
    if (new RegExp('(^|;)\\s*'+name+'=(.*?)(;|$)').test(document.cookie)) {
        return unescape(RegExp.$2);
    }
    return false;
}

function SetCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) + (expires ? "; expires=" + expires.toGMTString() : "") + (path ? "; path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "; secure" : "");
}

function btnProcessing() {

    $('.btn').live('mouseover',function(e){
        if(!$(this).hasClass('disabled')){
            $(this).addClass('btnhover');
        }
    }).live('mouseout', function () {
        if(!$(this).hasClass('disabled')){
            $(this).removeClass('btnhover');
        }
    }).live('mousedown',function(){
        if(!$(this).hasClass('disabled')){
            $(this).addClass('btndown');
            var btn = $(this);
            $('body').bind('mouseup',function(){
                btn.removeClass('btndown');
                $('body').unbind('mouseup');
            });
        }
    }).live('mouseup',function(){
        $(this).removeClass('btndown');
    });
}

/**
 * Обработка выпадающих списков
 */
function droplistProcessing() {
    //закрывать выпадающий список по щелчку в любом месте
    $('body').live('click', function(e){
        dropUp();
    });
    
    //не закрывать выпадающий список по щелчку на ползунке прокрутки
    $('.scroll-pane .jspDrag').live('click', function(){
        return false;
    });
    
    $('.droplist').click(function(){
        if($(this).is('.on')){
            dropUp();
            return false;
        }
        else{
            dropUp();
        }
        
        $('input:focus').blur();
        var thisdroplist = $(this);
        var itemsH = 0;
        var pane = $(this).next('.scroll-pane:first');
        var itemlist = pane.find('.itemlist');
        var pos = $(this).position();
        pane.css({
            'top':pos.top+8,
            'left':pos.left+1
        });
        itemlist.find('.listitem:last').css('border-bottom','none');
        $(this).addClass('on');
        itemlist.find('.listitem').each(function(){
            itemsH+=36;
        });
        if(itemsH<200){
            itemlist.height(itemsH);
            itemsH = 0;
        }
        else{
            itemlist.find('.listitem').each(function(){
                $(this).addClass('short');
            });
        }
        pane.show();
        itemlist.slideDown(200, function(){
            itemlist.jScrollPane(scrollParams);
        });
        return false;
    });
    $('.droplist').each(function() {
        var itemlist = $(this).next();
        if(itemlist.is('.scroll-pane')){
            var itemtext = itemlist.find('.listitem:first').text();
            var itemid = itemlist.find('.listitem:first').attr('id');
            $(this).find('.selected .selectinner a').text(itemtext).attr('rel',itemid);
        } 
    });
}

function dropUp(){
    $('.droplist').removeClass('on');
    $('.itemlist:visible, .scroll-pane:visible').slideUp(200);
}

swfobject.embedSWF("/header.swf", "logoflash", "728", "191", "9.0.0", false, false, 
{
    bgcolor:'#333333', 
    quality:'high'
}, {
    id: 'headflash', 
    style: GetCookie('HeadFlash_Show') == 'N' ? 'display:none' : ''
}, function(e) {
    if (!e.success) $('#removeflash').hide();
});
