/* scripts */

$(function(){
	$('.promo_close').click(function() {
		$('.d_promo').slideUp(500);
	});

	
	$('.dotted').click(function(){
		var href = $(this).attr('href').replace("#","");
		var block = $('a[name="'+href+'"]');
		if(block.length)
		{
			var top = block.offset().top;
			$('html,body').animate({scrollTop:top},500);
			$('#slide-about-a.slide-close').trigger('click');
			return false;
		}
	});
	
	$('.showCategoryInfo').live('click',function(){
		$('body').overlay().show();
		var catImage = $('#imageCategoryInfo');
		if(!catImage.length) { $('body').append('<img src="" id="imageCategoryInfo" style="display:none;"/>'); catImage = $('#imageCategoryInfo');}
		catImage.load(function(){
			$(this).proportions({window:'yes'}).middle('window').show();
		}).attr('src',$(this).attr('href'));
		return false;
	});

	$('#m5_overlay, #imageCategoryInfo').live('click',function(){
		$('#m5_overlay, #imageCategoryInfo').hide();
	});


    //print_my();
    //main_icons();
    fixmenu_ie6();
    firefox_opera_fix();
	
    $('li.rel19').width(190).css('margin-right','0px');
    $('li.rel22').width(190).css('margin-right','0px').find('img').css({
        height:'auto',
        width:23
    });
    $('li.rel23').width(290).css('margin-right','0px');
    $('li.rel21').width(290).css('margin-right','0px');
    $('li.rel24').width(263).css('margin-right','10px');
    $('li.rel26').width(250).css('margin-right','10px');
    $('li.rel25').width(195);

    if ($.browser.safari) {
        $('.per_rel21').css({
            left:631,
				top:186
        });
        $('.per_rel24').css({
            left:725
        });
    }

    if ($.browser.msie) {
        $('.per_rel21').css({
			left:631,
			top:186
        });
    }

/*

	$('.form input[type=text],.form textarea').focus(function(){
			$(this).addClass('input_on_print');
			$(this).prev('label').addClass('input_on_print');
			$(this).parent().addClass('checkbox_show_td');
	}).blur(function(){
		if($(this).val() == '')
		{
			$(this).removeClass('input_on_print');
			$(this).prev('label').removeClass('input_on_print');
			$(this).parent().removeClass('checkbox_show_td');
		}
	});

	$(".checkbox_more").click(function(event){
		$('.premise_table h5').parent().addClass('checkbox_show_td');
		if($(this).parent().is('.checkbox_show'))
		{	
			$(this).parent().parent().parent().parent().attr('id','');
			$(this).parent().parent().parent().parent().parent().removeClass('checkbox_show_td');
			if($(this).is('label'))
				$(this).parent().find('input').attr('checked','');
			var element = $(this).parent().removeClass('checkbox_show').parent().parent().parent().find('input, select').not('.checkbox');
			element.attr('disabled','disabled').eq(0).val('');
			element.find('option').eq(0).val('').text('').attr('selected','selected');
		}
		else
		{
			//$(this).parents('table').attr('id','checkbox_show_table');
			$(this).parent().parent().parent().parent().attr('id','checkbox_show_table').parents('table').attr('id','checkbox_show_table');
			$(this).parent().parent().parent().parent().parent().addClass('checkbox_show_td');
			if($(this).is('label'))
				$(this).parent().find('input').attr('checked','checked');
			var element = $(this).parent().addClass('checkbox_show').parent().parent().parent().find('input, select').not('.checkbox');
			element.removeAttr('disabled').eq(0).val(1);
			element.find('option').eq(0).val('Без упаковки').text('Без упаковки').attr('selected','selected');
		}


		if($(this).is('label'))
		return false; 
	});

*/


    if($(window).width() > 1024)
    {
        $('.cleft').addClass('ifwidth');
        $('.footer').addClass('ifwidthfooter');
    }
    else
    {
        $('.menu .main').css({
            paddingLeft:20,
            width:975,
            overflow:'hidden'
        });
        $('.navigation .title').css({
            paddingLeft:20
        });
        $('.logo').css('margin-left','20px');
        $('.cleft').removeClass('ifwidth');
		$('.cleftmain').width(420);
        $('.footer').removeClass('ifwidthfooter');
    }

	

    if (navigator.userAgent.indexOf('MSIE 6')!=-1) {
        var h=[];
        $('div.menu div.main ul table').each(function(i){
            h[i]=$(this).width();
        })
        for (j=0,p=0;j<h.length;j=j+2,p++) {
            k=Math.max(h[j],h[j+1]);
        //$('div.menu div.main ul li').eq(j).width(h[j]);
        //$('div.menu div.main ul li').eq(j+1).width(h[j+1]);
        //$('div.menu div.main ul').eq(p).width(k);
			  
        }
												 
    }
		
    $('.header .navi li:last').addClass('noBg');
    $('.header .navi .active').parent('li').addClass('noBg');
		
    $('.h5a a').click(function(){
        var index = $('.h5a').index($(this).parent('h5'));
        $(this).parent('h5').toggleClass('h5a_close').toggleClass('h5a_open');
        $('.h5p').eq(index).toggleClass('h5p_close').toggleClass('h5p_open');
        return false;
    });
		
    $('.form .input, .form .textarea').focus(function(){
        $(this).toggleClass('focus');
    });
    $('.form .input, .form .textarea').blur(function(){
        $(this).toggleClass('focus');
    });
	
    $('.pereezd a').not('.pereezd a.hover').css({
        opacity:0
    }).hover(function(){
        $(this).stop().addClass('hover').animate({
            opacity:1
        },300);
    },function(){
        $(this).stop().animate({
            opacity:0
        },0,function(){
            $(this).removeClass('hover')
        });
    });


    $('.header2 .menu li').hover(function(){
        $('.per_'+$(this).attr('class')).addClass('hover').animate({
            opacity:1
        },300);
    },function(){
        $('.per_'+$(this).attr('class')).animate({
            opacity:0
        },0,function(){
            $(this).removeClass('hover')
        });
    });


    /* gallery */
    var gallery2_width = $('.gallery table').width();
    var gallery2_shag = 685;
    var i = 0;
    $('.gallery .gallery_next').click(function(){
        if(gallery2_shag*(i+1) < (gallery2_width-gallery2_shag) && gallery2_width>gallery2_shag)
        {
            i++;
            $('.gallery table').animate({
                left:(gallery2_shag*i)*-1
            },700);
 
        }
        else
        {
            if(gallery2_shag*i < (gallery2_width-gallery2_shag))
            {
			
                i++;
                $('.gallery table').animate({
                    left:(gallery2_width-gallery2_shag)*-1
                },700);
            }
        //alert('df')
        }
    //alert(gallery2_width+' - '+gallery2_shag)
    });
    $('.gallery .gallery_prev').click(function(){
        if(i > 1)
        {
            i--;
            $('.gallery table').animate({
                left:+(gallery2_shag*i)*-1
            },700)
        }
        else
        {
            i = 0;
            $('.gallery table').animate({
                left:0
            },700)
        }
    });
/* gallery */
});

$(window).load(function() {
    my();
    $(window).resize(function() {
        my();
    })
	
	
    if ($('div.navigation').size()!=0) {
        if ($('div.navigation td.title img').size()!=0)
            var nav_h=$('div.navigation td.title img').height()
        else var nav_h=$('div.navigation').height()
			
        var top_=(nav_h-100)/2;
        $('div.links_new').css('top',top_).show();
    }
	
    $('.menu .active').corner("10px");
    $('div.content div.cright div.block').corner("10px");
    $('.menu ul:last').css('marginRight',0)
})

function firefox_opera_fix () {
    if (navigator.userAgent.indexOf('Firefox')!=-1 || navigator.userAgent.indexOf('Opera')!=-1) {
        $('.header2 .pereezd_1').css('left',235);
        $('.header2 .pereezd_5').css('left',631);
        $('.header2 .pereezd_6').css('left',725);
    }
}


function fixmenu_ie6 () {
/*if (navigator.userAgent.indexOf('MSIE 6')!=-1) {
		alert(ie6); //fix submenu ie6 :)
	}*/
}


function main_icons() {
	
    $('div.menu li[rel=25]').width(240);
    $('div.menu li[rel=25] a').width(205);
    var w=$(this).width();
						 
    if (w<1024) {
        /*$('a.besplatnie').css('left',890);
											   $('a.link2').css('left',890);
											   $('a.link4').css('left',890);
											   $('a.link3').css('left',890);
											   $('img.besplat_txt').css('left',830)*/
        $('table.index td:eq(0)').css('paddingLeft',20)
        $('div.main:eq(0) a:eq(0)').css('marginLeft',20);
    }
    else if (w>1024) {
        $('div.main:eq(0) a:eq(0)').css('marginLeft',0);
        $('table.index td:eq(0)').css('paddingLeft',0);
    /*$('img.besplat_txt').css('left',890+(w-1280)/5);
											   $('a.besplatnie').css('left',890+(w-1280)/5);
											   $('a.link2').css('left',890+(w-1280)/5);
											   $('a.link4').css('left',890+(w-1280)/5);
											   $('a.link3').css('left',890+(w-1280)/5);*/
    }
    $(window).resize(function(){
        var w=$(this).width();
										
        if (w<1024) {
            /*$('a.besplatnie').css('left',890);
											   $('a.link2').css('left',890);
											   $('a.link4').css('left',890);
											   $('a.link3').css('left',890);
											   $('img.besplat_txt').css('left',830)*/
            $('table.index td:eq(0)').css('paddingLeft',20)
            $('div.main:eq(0) a:eq(0)').css('marginLeft',20);
        }
        else if (w>1024) {
            $('div.main:eq(0) a:eq(0)').css('marginLeft',0);
            $('table.index td:eq(0)').css('paddingLeft',0);
        /*$('img.besplat_txt').css('left',890+(w-1280)/5);
											   $('a.besplatnie').css('left',890+(w-1280)/5);
											   $('a.link2').css('left',890+(w-1280)/5);
											   $('a.link4').css('left',890+(w-1280)/5);
											   $('a.link3').css('left',890+(w-1280)/5);*/
        }
    })
									
}

function print_my() {
    $('a.but_print_new').click(function() {
		//alert('test')
       window.print();
        return false;
    }
    )
}	

function my() {
    var body_w=($('body').width()-1000)/2;
    if (body_w>0) {
        extra=0
    }
    else
    {
        body_w=0;
        extra=40
    }
		
    if ($('body').width()>$('div.main td.title').width()+206+body_w/2) {
        if ($('div.main td.title img').size()!=0) left_=body_w+body_w/2+($('div.main td.title').width())
        else left_=body_w+body_w/2+754;
		
        $('div.links_new').css({
            'left':left_
        });
        $('div.links_new table').width(206+body_w/2+extra)
    }
}

function ToInt(x){
    x=x*1;
    if(!x)
        x=0;
    return x;
}


function str_replace (search, replace, subject, count) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    // %          note 1: The count parameter must be passed as a string in order
    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'

    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
    f = [].concat(search),
    r = [].concat(replace),
    s = subject,
    ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;
            }
        }
    }
    return sa ? s : s[0];
}

function clear_spaces(str){
    str=str_replace(" ","",str);
    str=str_replace("\n","",str);
    str=str_replace("\t","",str);

    return str;
}

function RecheckTime(time){
    var time_hour=time.substr(0,2);
    var time_minute=time.substr(3,2);
    if(time_hour>23){							
        time_hour=23;
    }
    if(time_minute>59){
        time_minute=59;
    }

    return time_hour+':'+time_minute;
}

function RecheckDate(date){
    var day=date.substr(0,2);
    var month=date.substr(3,2);
    var year=date.substr(6,4);
    
    var today = new Date();
    tyear = today.getFullYear();
    
    if(day>31){							
        day=31;
    }
    if(month>12){
        month=12;
    }
    if(year<2010 || year>(tyear+2)){
        year=2010;
    }

    return day+'.'+month+'.'+year;
}

function CheckEmail(email_value){
    reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
    if (!email_value.match(reg)) {
        return false;
    }

    return true;
}


function ShowPopup(html){	
    var popup_content=$(".popup_content");
	popup_content.html(html);
	
	var show_category = $(".show_category");
	show_category.css("margin-left","-200px");
	show_category.css("margin-top","-200px");
	
	$(".hide_all").fadeTo('fast', 0.8);	
	$(".show_category").fadeIn('fast');
	
	var img = $('.popup_content>img');
	if(img.length>0){
		img.load(function(){		
			$(this).removeAttr("width")
			       .removeAttr("height")
	               .css({ width: "", height: "" }); 
			 img.proportions({window:'yes',height:20});
			 show_category.middle('window',{
				width:img.width(),
				height:img.height()
			});
		    //var width  = $(this).width();
		    //var height = $(this).height();	
		    //show_category.css("margin-left","-"+width/2+"px").css("width",width);
		    //show_category.css("margin-top","-"+height/2+"px").css("height",height);
			
	
		});
		
		var src  = img.attr('src');
	    img.attr('src', '');
	    img.attr('src', src);
    }
    else{
        var width = popup_content.width();
        var height = popup_content.height();
    	show_category.css("margin-left","-"+width/2+"px").css("width",width);
		show_category.css("margin-top","-"+height/2+"px").css("height",height);
    }

	
   
}

function HidePopup(){ 	
    $(".hide_all,.show_category").fadeOut('fast',function(){
    	 $(".popup_content").html('');
    });
   
}


