var path = document.location.pathname;
if (path.charAt(path.length-1) != '/') 
        path = path+'/';

function ie6_width(min,max){
        w = document.documentElement.clientWidth;
        return (w <= min) ? min + "px" : ((w >= max) ? max + "px" : "auto");
}

var simpleUppod = {
        load: function(obj_id, swf_path, flv_path, skin_path, flv_name, w, h){
                var width = (w != undefined ) ? w : 320;
                var height = (h != undefined ) ? h : 240;
                if(obj_id == undefined) alert("obj_id - is undefined");
                if(swf_path == undefined) alert("swf_path - is undefined");
                if(flv_path == undefined) alert("flv_path - is undefined");
                if(skin_path == undefined) alert("skin_path - is undefined");
                str = '';
                //  if( flv_name != undefined )
                //    str += '<div class="title">'+flv_name+'</div>';
                str += '<object classid="clsid27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'" id="spin_video" align="middle">';
                str += '<param name="allowScriptAccess" value="sameDomain" />';
                str += '<param name="allowFullScreen" value="true" />';
                str += '<param name="movie" value="'+swf_path+'" />';
                str += '<param name="menu" value="false" />';
                str += '<param name="quality" value="high" />';
                str += '<param name="wmode" value="transparent" />';
                str += '<param name="bgcolor" value="#fff" />';
                str += '<param name="FlashVars" value="st='+skin_path+'&file='+flv_path+'&autolay" />';
                str += '<embed src="'+swf_path+'" FlashVars="st='+skin_path+'&file='+flv_path+'&autolay" menu="false" quality="high" wmode="transparent" bgcolor="#fff" width="'+width+'" height="'+height+'" name="spin_video" align="middle" allowScriptAccess="sameDomain" allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
                str += '</object>';
                $("#"+obj_id).empty().append(str);
        }
}

var site = {
	
        index : -1,
        timer : null,
	 
        init : function() {
                $('#broni a').click(function(){

                        temp=$(this).attr('rel')
                        $('input.'+temp).focus()
                        return false;
                })
                $('#_bron').click(function(){
                        $('#mini_bron').submit()
                })

                
                
                var nv = navigator.appName, ua = navigator.userAgent;
                //$('.icon-bb').picbox({
                //	range : 111,
                //	margin : true
                //});
                try {
                        $('.lightbox').tinyBox();
		
                        $('.i-smbox').picbox({
                                range : 104
                        });

                        //зебра в таблице рабочего поля
                        $('.content .visual-table tr:nth-child(2n+2) td').addClass('dark')
                        
                        $('.i-wr').picbox({
                                range : 102
                        })
                        $('.b-conf').picbox({
                                range : 104,
                                position : 'v',
                                preview : false,
                                onSlide : function( i, owner, cursor ) {
                                        $('.slider-b', owner).animate({
                                                scrollTop : i * 258
                                        });
                                }
                        })
                        $('.iv-box').picbox({
                                range : 170,
                                position : 'v',
                                offset_calc : true,
                                preview : false
                        })
                        $('.ih-box').picbox({
                                range : 104,
                                position : 'h',
                                preview : false
                        })
                        //$('.reflect').reflect({ height: 0.23 });
                        $('form .conf').change(function() {
                                var $form = $(this).parents('form');
                                var id = $(this).attr('rel');
                                if ($(this).is(':checked')) {
                                        $form.find('#' + id).removeClass('disabled');
                                } else {
                                        $form.find('#' + id).addClass('disabled');
                                }
                        });
                        $('.send:not(.enabled)').addClass('disabled');
                        $('.send:not(.disabled)').live('click', function() {
                                var $form = $(this).parents('form');
                                var $root = $form.parent();
			
                                if ($(this).hasClass('disabled')) {
                                        $('#done', $root).show().find('td').html("Пожалуйста подтвердите согласие с обработкой Ваших персональных данных");
                                        setTimeout(function(){
                                                $('#done', $root).fadeOut(200);
                                        }, 6000);
                                } else {
                                        $.ajax({
                                                type: $form.attr('config:method'),
                                                url: $form.attr('config:action'),
                                                data: $form.serialize(),
                                                dataType: 'json',
                                                beforeSend: function(){
                                                        $form.find('input').attr('disabled', true);
                                                },
                                                success: function(data){
                                                        switch (data.state) {
                                                                case 'error':
                                                                        $('#done', $root).show().find('td').html(data.message.join('<br/>'));
                                                                        break;
                                                                case 'done':
                                                                        $('#done', $root).show().height($form.height()).addClass('mes-highlight').find('td').html(data.message);
                                                                        $form.hide();
                                                                        break;
                                                        }
						
                                                        $form.find('input').attr('disabled', false);
                                                        if (data.state == 'error') {
                                                                setTimeout(function(){
                                                                        $('#done', $root).fadeOut(200);
                                                                }, 6000);
                                                        }
                                                }
                                        });
                                }
                                return false;
                        });

                        $('.menu dl dt').each(function() {
                                var max_width = 0;
                                var $sub = $(this).find('.sub-level:eq(0)');
                                $sub.find('a').each(function() {
                                        if( max_width < $(this).width()) {
                                                max_width = $(this).width();
                                        }
                                });
                        $sub.width( max_width + 90);
                        });
                $('.menu dl dt').hover(function() {
                        if( $(this).hasClass('have-child') ) {
                                $('.sub-level', this).show();
                                $(this).addClass('active-parent');
                                clearTimeout(this.timer);
                        } else {
                                $(this).filter(':not(.current)').addClass('active');
                        }
                }, function() {
                        var t = this;
                        if( $(this).hasClass('have-child') ) {
                                this.timer = setTimeout(function() {
                                        $('.sub-level', t).hide();
                                        $(t).removeClass('active-parent');
                                }, 700);
                        } else {
                                $(this).filter(':not(.current)').removeClass('active');
                        }
                });
		
		$('.sub-in.selected').prev().find('a').addClass('no-bg');
               
                $('.sub-v .sub-in').not('.selected').hover(function() {
                        $(this).prev().find('a').addClass('no-bg');
                        $(this).addClass('active');
                }, function() {
                        $(this).prev().find('a').removeClass('no-bg');
                        $(this).removeClass('active');
                });
		
                $('.sub-v li.have-child a.child').toggle(function() {
                        $(this).parent().find('ul').slideDown();
                        $(this).parent().addClass('cover');
			
                        return false;
                }, function() {
                        $(this).parent().removeClass('cover').find('ul').slideUp();
                        return false;
                });
        } catch( e ) {}
		
        /*$('form.validate').validate({
				hightlight_error: function($field, error_type) {
					
					var initialized = $field.data('initialized');
					$field.addClass('field-error')
					
					if (!initialized) {
						$field.wrap('<div class="field-wrapper"></div>')
						$field.focus(function(){
							$field.removeClass('field-error');
							$field.parent().find('.field-error-message').remove();
						});
					}
					
					if ($field.attr('data-fv-' + error_type + '-error')) {
						$field.after('<div class="field-error-message">' + $field.attr('data-fv-' + error_type + '-error') + '</div>');
					}
			
					$field.data('initialized', true);
			
					setTimeout(function(){
						$field.removeClass('field-error');
						$field.parent().find('.field-error-message').remove();
					}, 5000);
				}
		});*/
		
		
		
        function hours() {
                var v = parseInt($(this).val());
                v = isNaN(v) ? 0 : v;
                var id = $(this).attr('rel');
                if( v > 23 ) {
                        $(this).val('00');
                } else {
                        var val = v >= 10 ? v : '0' + v;
                        $(this).val(val);
                }
                if( $('#'+id).val() == '' || $('#' + id).val() == undefined ) {
                        $('#'+id).val('00');
                }
        }
        $('.hours').blur(hours).change(hours);
			
        function mints() {
                var v = parseInt($(this).val());
                var id = $(this).attr('rel');
                v = isNaN(v) ? 0 : v;
                var val = v >= 10 ? v : '0' + v;
				
                $(this).val(val);
                if( v > 59 ) {
                        var old_v = parseInt($('#'+id).val());
					
                        var val = old_v >= 10 ? old_v + 1 : '0' + (old_v + 1);
                        $('#'+id).val(old_v + 1 >= 23 ? '00' : val);
                        $(this).val('00')
                }
                $('#'+id).blur();
        }
        $('.mints').blur(mints).change(mints);
			
        $('.hours').keydown(function(e) {
                var v = parseInt($(this).val());
                var nv = isNaN(v) ? 0 : v;
                var mod = 0;
				
                switch(e.keyCode) {
                        case 38: // up
                                nv += 1;
                                mod = 1;
                                break;
						
                        case 40: // down
                                nv -= 1;
                                mod = 1;
                                break;
                }
                nv = nv < 0 ? 24 + nv : nv;
                if (mod) {
                        if (nv > 23) {
                                $(this).val('0');
                        }
                        else {
                                $(this).val(nv);
                        }
                }
        });
			
        $('.mints').keydown(function(e) {
                var v = parseInt($(this).val());
                var id = $(this).attr('rel');
                v = isNaN(v) ? 0 : v;
				
                switch(e.keyCode) {
                        case 38: // up
                                v += 1;
                                break;
						
                        case 40: // down
                                v -= 1;
                                break;
                }
                $(this).val(v);
                if( v > 59 ) {
                        var old_v = parseInt($('#'+id).val());
					
                        $('#'+id).val(old_v >= 23 ? '00' : old_v + 1);
                        $(this).val('00')
                } else if( v < 0 ) {
                        var old_v = parseInt($('#'+id).val());
					
                        $('#'+id).val(old_v <= 0 ? 23 : old_v - 1);
                        $(this).val(59 + v);
                }
        });
		
        $('.bron-form').each(function() {
                var t = this;
                $('.bron-main .tab a:not(.active)').live('click', function() {
                        var i = $('.tab a').index(this);
                        $('dd', t).eq(i).slideDown();
                        $('.tab a').eq(i).addClass('active');
                });
			
                $('#conf').change(function() {
                        if ($(this).is(':checked')) {
                                $('#send').removeClass('disabled');
                        } else {
                                $('#send').addClass('disabled');
                        }
                });
			
                $('#close', t).click(function() {
                        var p = $(this).parents('dd');
                        var i = $('dd', t).index(p);
                        p.slideUp(function() {
                                $('.tab a').eq(i).removeClass('active');
                        });
                });
			
                $('#reset', t).click(function() {
                        var p = $(this).parents('form');
                        p.get(0).reset();
                });
			
                $('#send:not(.disabled)', t).addClass('disabled').live('click', function() {
                        var $form = $(this).parents('form');
                        //$form.submit();
                        var $root = $form.parent();
				
                        //	if ($form.hasClass('xv-valid')) {
                        $.ajax({
                                type: $form.attr('config:method'),
                                url: $form.attr('config:action'),
                                data: $form.serialize(),
                                dataType: 'json',
                                beforeSend: function(){
                                        $form.find('input').attr('disabled', true);
                                },
                                success: function(data){
                                        switch (data.state) {
                                                case 'error':
                                                        $('#done',$root).show().find('td').html(data.message.join('<br/>'));
                                                        break;
                                                case 'done':
                                                        $('#done', $root).show().height($form.height()).addClass('mes-highlight').find('td').html(data.message);
                                                        $form.hide();
                                                        break;
                                        }
							
                                        $form.find('input').attr('disabled', false);
                                        if (data.state == 'error') {
                                                setTimeout(function(){
                                                        $('#done', $root).hide();
                                                }, 8000);
                                        }
                                }
                        });
                        //}
                        return false;
                });
        });
},
	
slider : {
        config : {
                cur : 0,
                size : 0,
                slider : null,
                vert : true,
                range : 84
        },
        slider : null,
		
        init : function( sel, s ) {
                var t = $(sel), self = this;
                site.slider.slider = t;
			
                site.slider.config = $.extend(true, site.slider.config, s );
                site.slider.config.size = $('.i-push', t).length;
			
			
                $('#prev:not(.disabled)', t).live('click', function() {
                        $('#next.disabled', t).removeClass('disabled');
                        $('#end.disabled', t).removeClass('disabled');
                        if( site.slider.config.cur-5 == 0 ) {
                                $(this).addClass('disabled');
                                $('#home', t).addClass('disabled');
                        }
                        if( site.slider.config.cur-5 >= 0 ) {
                                site.slider.run( site.slider.config.cur-5 );
                        }
                        return false;
                });
			
                $('#next:not(.disabled)', t).live('click', function() {
                        $('#prev.disabled', t).removeClass('disabled');
                        $('#home.disabled', t).removeClass('disabled');
				
                        if( site.slider.config.cur + 5 < site.slider.config.size -1 ) {
                                site.slider.run( site.slider.config.cur+5 );
                        }
                        if( site.slider.config.cur + 5 > site.slider.config.size - site.slider.config.cur) {
                                $(this).addClass('disabled');
                                $('#end', t).addClass('disabled');
                        }
                        return false;
                });
			
                $('#home:not(.disabled)', t).click(function() {
                        site.slider.run( 0 );
                        $('#end', t).removeClass('disabled');
                        $('#next', t).removeClass('disabled');
                        $('#prev', t).addClass('disabled');
                        $(this).addClass('disabled');
                        return false;
                });
			
                $('#end:not(.disabled)', t).click(function() {
                        site.slider.run( site.slider.config.size - 1 );
                        $('#home', t).removeClass('disabled');
                        $('#prev', t).removeClass('disabled');
                        $('#next', t).addClass('disabled');
                        $(this).addClass('disabled');
                        return false;
                });
        },
		
        run : function( i ) {
                var $car = $('.list-car', site.slider.slider );
			
                $car[site.slider.config.vert ? 'scrollTop' : 'scrollLeft']( Math.floor(i * site.slider.config.range) );
                site.slider.config.cur = i;
        }
},

calendar: {
        options: {
                block: null,
                selected_month: null,
                selected_year: null,
                news_days: null,
                week_days: ['пн','вт','ср','чт','пт','сб','вс'],
                months: ['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь']
        },
		
        init: function( ) {
                $block = site.calendar.options.block = $("#block-calendar");
                site.calendar.options.selected_year = site.calendar_init.year.current;
		
			
                site.calendar_init.callback = site.calendar_init.callback || function(b, c) {};
			
                $('.m-sel').click(function(){
				
				
                        if( $(this).hasClass('next') ) {
                                site.calendar_init.month.current = site.calendar_init.month.current + 1;
                        } else if( $(this).hasClass('prev') ){
                                site.calendar_init.month.current = site.calendar_init.month.current - 1;
                        }
				
                        if( site.calendar_init.month.current == 13 ) {
                                site.calendar_init.month.current = 1;
                        } else if(site.calendar_init.month.current == 0 ) {
                                site.calendar_init.month.current = 12;
                        }
                        $block.find('.title').html(site.calendar.options.months[site.calendar_init.month.current-1]);
                        site.calendar_init.callback($block, site.calendar.options, site.calendar_init.month.current-1);
                        site.calendar.update();
                        return false;
                });
			
                $block.find(".month div").live("mouseover",function(){
                        $(this).addClass("selected");
                }).live("mouseout", function(){
                        $(this).removeClass("selected");
                });
                site.calendar_init.callback($block, site.calendar.options, site.calendar_init.month.current-1);
                site.calendar.update();
        },
        update: function() {
                var $block = site.calendar.options.block;
                site.calendar.options.selected_month = site.calendar_init.month.current-1;
			
                $block.find(".month").hide();
			
                $.ajax({
                        type: "get",
                        dataType: "json",
                        url: "/ajax/?action=match&month="+site.calendar.options.selected_month+"&year="+site.calendar_init.year.current,
                        beforeSend: function() {
                        },
                        success: function(data){
                                site.calendar.options.days = data;
                                site.calendar.generate();
                                $block.find(".month").fadeIn();
                        },
                        error: function() {
                        }
                });
        },
        generate: function() {
                var $block = site.calendar.options.block;
			
                var date = new Date();
                date.setDate(1);
                date.setMonth(site.calendar.options.selected_month);
                date.setFullYear(site.calendar.options.selected_year);
			
                var dayCount = 32 - new Date(
                        site.calendar.options.selected_year,
                        site.calendar.options.selected_month-1,
                        32).getDate();
			
                var month = '<table cellspacing="1">';
                for (var i=0; i<7; i++) {
                        month += '<th>'+site.calendar.options.week_days[i]+'</th>\n';
                }
		    
                var start = (date.getDay()-1)%7;
                if (start <= 0)
                        start += 7;

                month += '<tr>';
			
                for (var i=start; i>0; i--) {
                        month += '<td><span class="text">'+(dayCount-i+1)+'</span></td>';
                }
		    
                var month_length = new Date(date.getFullYear(), 1+date.getMonth(), 0).getDate();
		    
                var current = '';
                var curext = '';
                var selected_month = site.calendar.options.selected_month+1;
                var selected_year = site.calendar.options.selected_year;
                var diff = 1;
                for (var i=1; i<=month_length; i++) {
                        if ((start%7) == 0 && start > 0) {
                                month += '</tr><tr>';
                                diff++;
                                start = 0;
                        }
		    	
                        current = '';
                        curext = '';
                        if (i == site.calendar_init.day.current && site.calendar.options.selected_month+1 == site.calendar_init.month.current) {
                                current = ' class="current"';
                                curext = 'current';
                        }
				
		       
                        if( site.calendar.options.days[i] ) {
                                var scet = site.calendar.options.days[i]['scet'];
                                var type =  site.calendar.options.days[i]['type'];
                                var id =  site.calendar.options.days[i]['id'];
                                var title =  site.calendar.options.days[i]['title'];
                                var logo = '';
                                if( site.calendar.options.days[i]['photo']['small'] !== null && site.calendar.options.days[i]['photo']['small'] !== undefined ) {
                                        logo = '<img height="34" width="34" src="'+'/files/image/match/' + site.calendar.options.days[i]['photo']['small']+'">';
                                }
                                switch(type ) {
                                        case 1:
                                                month += '<td class="month-day-p '+curext+'"><a href="/match/?id='+id+'" title="'+title+'"><span class="date">' + i + '</span><span class="scet">' + scet + '</span><span class="type">П</span>'+logo+'</a>';
                                                break;
                                        case 2:
                                                month += '<td class="month-day-v '+curext+'"><a href="/match/?id='+id+'" title="'+title+'"><span class="date">' + i + '</span><span class="scet">' + scet + '</span><span class="type">П</span>'+logo+'</a>';
                                                break;
                                }
                        } else {
                                month += '<td class="month-day '+curext+'">';
                                month += '<span class="date">' + i + "</span>";
                        }
                        month += '</td>';
                        start++;
                }
                date.setDate(i-1);
                var end = date.getDay()%7;
                if ((start%7) == 0 && start > 0) {
                        month += '</tr><tr>';
                        diff++;
		    
                }
                if (end < 0)
                        end += 7;
			
                var n = diff == 6 ? 0 : 7;
			
                for(var i = 0; i < 7 - end + n; i++) {
                        if ((start%7) == 0 && start > 0) {
                                month += '</tr><tr>';
                                start = 0;
                        }
                        start++;
		    	
                        month += '<td><span>'+(i+1)+'</span></td>';
                }
			
                month += '</tr></table>';
		    
                $block.find(".month").html(month);
        }
},
	
tree: {
        init: function() {
                $(".ajax-tree a").live("click", site.tree.onAjaxTreeNodeClick);
                $(".ajax-tree a").each(function(){
                        var href = $(this).attr("href").replace('http://'+document.domain,'');
                        if ( href.charAt(href.length-1) != '/' ) href = href+'/';
				
                        if (path.indexOf(href) == 0) {
                                $(this).parent().addClass("selected");
                                $(this).addClass('active');
                                Cufon.replace('.active', {});
                        }
				
                        if (document.location.pathname == '/')
                                return;
				
                        if (path.indexOf(href) == 0)
                                $(this).click();

                });
        },
        onAjaxTreeNodeClick: function(e) {
                var id = $(this).attr("rel");
			
                if ( !id || $(this).hasClass('disabled-click')) return;
			
                var ajax_tree = $(this).parents(".ajax-tree");
			
                if( $(this).parent().hasClass('opened') ) {
                        $(this).parent().removeClass('opened');
                        $(this).removeClass('active');
                } else {
                        $(this).parent().addClass('opened');
                        $(this).addClass('active');
                }
			
                if ( ajax_tree.data("state-"+id) != null ) {
                        if (ajax_tree.data("state-" + id) == true) {
                                $(this).removeClass('current');
                                $("#ajax-tree-node-" + id).slideUp();
                                if( $(this).parents('.level0').hasClass('level0') ) {
                                        $(this).parents('.level0').removeClass('pad15');
                                }
					
                        } else {
                                $(this).addClass('current');
                                $("#ajax-tree-node-" + id).slideDown();
                                if( $(this).parents('.level0').hasClass('level0') ) {
                                        $(this).parents('.level0').addClass('pad15');
                                }
                        }
					
                        ajax_tree.data("state-"+id, !ajax_tree.data("state-"+id));
                        return false;
                } else {
                        if( $(this).parents('.level0').hasClass('level0') ) {
                                $(this).parents('.level0').addClass('pad15');
                        }
                }

                $(this).addClass('current');
                $('<ul id="ajax-tree-node-'+id+'"><li class="last" style="padding: 5px;">Загрузка...</li></ul>').insertAfter($(this));
			
                $.get(ajax_tree.attr("tree:url")+id,{}, function(data) {
                        $("#ajax-tree-node-"+id+" li").remove();
                        for( i in data.nodes ) {
                                $("#ajax-tree-node-"+id).append('<li class="node-level'+data.nodes[i].level+'"><a class="level'+data.nodes[i].level+'" href="'+data.nodes[i].path+'"'+(parseInt(data.nodes[i].have_childs) > 0 ? ' rel="'+data.nodes[i].id+'"':'')+'><span>'+data.nodes[i].title+'</span></a></li>')
                        }
				
                        $("#ajax-tree-node-"+id+" a").each(function () {
                                var href = $(this).attr("href").replace('http://'+document.domain,'');
					
                                if ( $(this).attr("rel") > 0 ) {
                                        if ( path.indexOf(href) == 0 )
                                                $(this).click();
                                }
                                else if ( path.indexOf(href) == 0 )
                                        $(this).addClass("current");
                        }
                        );

                        $("#ajax-tree-node-"+id+" li:last").addClass("last");
				
                        ajax_tree.data("state-"+id, !ajax_tree.data("state-"+id));
                },"json");
			
                return false;
        },
        closeAjaxTreeNode: function(e) {
                return false;
        }
}
};

function __() {
        try {
                console.log(__.caller, arguments);
        } catch (e) {}
}

$(site.init);
