var timeout = new Array;
var sld_interval;
var sld_stop = 0;
var sld = 1;
$(document).ready( function (){ 
	$(document).pngFix();
	//$(".button_popup").hover(function(){ var id = getId( $(this).attr('id') ); clearTimeout(timeout[id+'o']); clearTimeout(timeout[id+'p']); timeout[id+'h'] = setTimeout(function(){ button_popup_hover(id) }, 333) }, function(){ var id = getId( $(this).attr('id') ); clearTimeout(timeout[id+'h']); timeout[id+'o'] = setTimeout(function(){button_popup_out(id)}, 333)})
	$(".button_popup").hover(function(){ var id = getId( $(this).attr('id') ); clearTimeout(timeout[id+'o']); clearTimeout(timeout[id+'p']); timeout[id+'h'] = setTimeout(function(){ button_popup_hover(id) }, 333) }, function(){ var id = getId( $(this).attr('id') ); clearTimeout(timeout[id+'h']); timeout[id+'o'] = setTimeout(function(){  }, 333)})
	$(".button_popup_stat").live("click", function(e){ e.preventDefault(); button_popup_stat(getId($(this).attr('id'))); })
	$(".ajax-img").live("click", function(e){ e.preventDefault(); doLoad( $(this).attr('href'), getId($(this).attr('id')), '', 2, 1); });
	$(".ajax").live("click", function(e){ e.preventDefault(); var t = $(this).attr('id').split('_'); if( t[2] != undefined ) id = t[1]+'_'+t[2]; else id = t[1]; $('#box_'+t[1]+'_'+t[2]+'_db').html('<img src="'+img+'/site/uns/ok.gif" alt="" width="14" height="14" border="0" align="bottom" />'); doLoad( $('#link_'+id).attr('href'), 'box_'+t[1], 'form_'+id, 2, 2); });
	$(".ajax-slider").live("click", function(e){ e.preventDefault(); var t = $(this).attr('id').split('_'); clearInterval(sld_interval); if( t[2] != undefined ) id = t[1]+'_'+t[2]; else id = t[1]; doLoad( $('#link_'+id).attr('href'), '', '', 2, 22); });
	$(".img").live("click", function(){ doLoad( $('#lnkpop_'+getId($(this).attr('id'))).attr('href'), '', '', 2, 4); });
	$(".preview").live("click", function(e){ e.preventDefault(); var id = getId( $(this).attr('id') ); doLoad( $('#link_'+id).attr('href'), '', '', 2, 4); });
	$(".next-img").live("click", function(e){ e.preventDefault(); doLoad( $(this).attr('href'), '', '', 2, 5); });
	$(".popup-prevent-defolt").live("click", function(e){ e.preventDefault() });
	$(".hd").hide();
	$(".op").live("click", function(){ $('#r_'+$(this).attr('id')).toggle() });
	$(".ch").live("click", function(){ var d = $(this).attr('id').split('_'); if($(this).attr('src') == 'http://img.monchul/site/check1.gif'){ $('.rc'+d[1]).attr("checked", ""); $(this).attr('src', 'http://img.monchul/site/check3.gif'); } else { $(this).attr('src', 'http://img.monchul/site/check1.gif'); $('.rc'+d[1]).attr("checked", "checked"); } });
	$(".ajax_form").live('submit', function(e){ e.preventDefault(); var id = getId( $(this).attr('id') ); doLoad( $(this).attr('action'), id, $(this).serialize(), 2, 3); link_popup_remove(); });
	$(".link-popup").live("click", function(e){ e.preventDefault(); link_popup( getId( $(this).attr('id') ) ); });
	$(".tg").live("click", function(e){ $('.tg').removeClass('yellow').addClass('blue');; $(this).removeClass('blue').addClass('yellow'); });
	$("form#form_search input[type=radio]").live("click", function(){ $("form#form_search").attr('action', '/'+lang+'/'+$(this).val()+'/search.html'); });
	$(window).resize( function(){ rsz() })
});
///////////////////////////////////////////////////////////////////////////////////////////

$(document).ready( function (){ if ( $('#box_start').is('#box_start') ) { sld_interval = setInterval( function(){slider(sld)}, 15000); } });
function slider (fl){
sld++;if(sld>4)sld=1;
doLoad( '/'+lang+'/mdstart/'+sld+'.html', '', '', 2, 22);
} 
//////////////////////////////////////////////////////////////////////////////////////////
function getId(id){
	var t = id.split('_'); return t[1];
}

function button_popup_stat( id ){
	if ( $('#popup_'+id).length ) return;
	var offset = $( '#img_'+id ).offset();
	var fl = '';
	var lf = 374;
	var tp = 44;
	if(id == 'search'){ fl = '-search'; lf = 600; tp = -84; }
	$('body').append('<div id="popup_'+id+'"></div>');
	$('#popup_'+id).addClass('button-popup-box'+fl).css({ left : (offset.left - lf)+'px', top : (offset.top + tp)+'px'});
	doLoad ('/'+lang+'/popup/'+id+'/'+component+'/'+catalog+'/'+product, 'popup_'+id, '', 2, 0);
}

function button_popup_hover(id){
	$('.button-popup-box').remove();
	if ( $('#popup_'+id).length ) return;
	var offset = $( '#img_'+id ).offset();
	var fl = '';
	var lf = 374;
	var tp = 44;
	if(id == 'search'){ fl = '-search'; lf = 580; tp = -34; }
	$('body').append('<div id="popup_'+id+'" class="button-popup-box'+fl+'"></div>');
	$('#popup_'+id).css({ left : (offset.left - lf)+'px', top : (offset.top + tp)+'px'})
	$('#popup_'+id).hover( function(){ clearTimeout(timeout[id+'o']) }, function(){  })
	//$('#popup_'+id).hover( function(){ clearTimeout(timeout[id+'o']) }, function(){ timeout[id+'p'] = setTimeout(function(){button_popup_out(id)}, 333) })
	doLoad ('/'+lang+'/popup/'+id+'/'+component+'/'+catalog+'/'+product, 'popup_'+id, '', 2, 0);
}

function link_popup(id){

	var rel = $('#link_'+id).attr('rel');
	var ln = $('a[rel='+rel+']');
	for (var i = 0; i < ln.length; i++){
		var idd = getId( $(ln[i]).attr('id') );
		$('#popup_'+idd).remove();
	}
	var offset = $( '#link_'+id ).offset();
	var lf = 8;
	var tp = 16;
	
	$('body').append('<div id="popup_'+id+'"></div>');
	var win = getWindowWidth();
	if(win < (offset.left + 446)) lfw = (win - 446)
	else lfw = (offset.left - lf)
	$('#popup_'+id).addClass('link-popup-box').css({ left : lfw+'px', top : (offset.top + tp)+'px'})
	doLoad ( $('#link_'+id).attr('href'), 'popup_'+id, '', 2, 0);
}

function link_popup_remove( id ){ $('#popup_'+id ).remove(); $('#popup_'+id ).remove(); $('#img_box').remove(); $('#body_wr').fadeTo(222, 0, function(){ $(this).remove(); }); }

function button_popup_out(id){ $('#popup_'+id).remove(); }
function remove_popup(){ $('.link-popup-box').remove(); }
function button_popup_remove(){ $('.button-popup-box' ).remove(); $('.button-popup-box-search' ).remove(); }

function doLoad (u, b, f, a, q) {
	arr = new Array(0,1,2,3);
	arr[0] = new Array(0,1,2);
	arr[1] = new Array(0,1,2);
	arr[2] = new Array(0,1,2);
	arr[3] = new Array(0,1,2);
	arr[0][0] = 'html';
	arr[0][1] = true;
	arr[0][2] = 'get';
	arr[1][0] = 'json';
	arr[1][1] = true;
	arr[1][2] = 'get';
	arr[2][0] = 'html';
	arr[2][1] = false;
	arr[2][2] = 'post';
	arr[3][0] = 'json';
	arr[3][1] = false;
	arr[3][2] = 'post';
	if(sld_stop == 1) return;
	var img0;
		if(f != ''){
		var ff = document.getElementById(f);
		if(f.indexOf('=') == -1) var sd = $('#div_'+f).find('form').serialize();
		else var sd = f;
	}
	$.ajax({
		url: u,
		dataType : arr[a][0],
		data : sd,
		cache : arr[a][1],
		type : arr[a][2],
		beforeSend: p,// до отправки
		success:  s//после отправки
	});
	
	function p()
	{// до отправки
		$('#load').attr( 'src', 'http://img.monchul/site/load.gif' );
		if(q == 1){
			$('#img_'+b).attr( 'src', 'http://img.monchul/site/load.gif' );
		}
		if(q == 4){
			$('#img_box').remove();
			$('#body_wr').remove();
			$('body').append('<div id="body_wr" onClick="link_popup_remove( 0 )" style="background-color: #000000; position:absolute;"></div>');
			$('#body_wr').fadeTo(0, 0).css({left:"0px",top:"0px",height:$(document).height()+'px',width:$('#mn').width()+'px'}).fadeTo(222, 0.1);
		}
	}
	
	function s(data){
		$('#load').attr( 'src', 'http://img.monchul/site/spacer.gif' );
		if(q == 0){
			$("#"+b).html(data);
			if(b == 'popup_search') $('#s').focus();
		}
		if(q == 1){
			$('#img_'+b).attr( 'src', 'http://img.monchul/site/spacer.gif' );
			doLoad( '/'+lang+'/user/1.html', 'box_user', '', 2, 2)
		}
		if(q == 2){
			$("#"+b).html(data);
			if(b == 'box_tree') remove_popup();
		}
		if(q == 22){
			sld_stop = 1;
			$("#box_start_hide").html(data);
			var offset = $('#box_start').offset();
			$('#box_start_hide').fadeTo(0, 0).css({ left : offset.left + "px", top : offset.top + "px"} ).fadeTo(1000, 1, function(){ $('#box_start').html($('#box_start_hide').html() ); sld_stop = 0;
			$('#box_start_hide').css({ left : "-1000px", top : "-1000px"})});
		}
		if(q == 3){
			$("#box_"+b).html(data);
		}
		if(q == 4){
			$('body').append('<div id="img_box" style="top: -5000px; left: -5000px;  position:absolute; "></div>');
			$('#img_box').html(data);
			rsz()
		}
		if(q == 5){
			$('#img_box').html(data);
		}
	}
}

function rsz(){
	$('#body_wr').css( { height:$(document).height()+'px',width:$('#mn').width()+'px' })
	var tp = ((window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0) + ((getWindowHeight() - $('#img_rel').height()) / 2))
	var lft = ((window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0)+((getWindowWidth() - $('#img_rel').width()) / 2))
	if(tp < 20) tp = 20;
	$('#img_box').css({ left: lft+"px", top: tp+"px", height: ($('#img_rel').height() + 4)+"px", width: ($('#img_rel').width() + 4)+"px" });//
}

function getWindowHeight() {
  var windowHeight=0;
  if (typeof(window.innerHeight)=='number') {
    windowHeight=window.innerHeight;
  }
  else {
    if (document.documentElement&&document.documentElement.clientHeight) {
      windowHeight=document.documentElement.clientHeight;
    }
    else {
      if (document.body&&document.body.clientHeight) {
        windowHeight=document.body.clientHeight;
      }
    }
  }
  return windowHeight;
}

function getWindowWidth() {
  var windowWidth=0;
  if (typeof(window.innerWidth)=='number') {
    windowWidth=window.innerWidth;
  }
  else {
    if (document.documentElement&&document.documentElement.clientWidth) {
      windowWidth=document.documentElement.clientWidth;
    }
    else {
      if (document.body&&document.body.clientWidth) {
        windowWidth=document.body.clientWidth;
      }
    }
  }
  return windowWidth;
}

function number_format (number, decimals, dec_point, thousands_sep)
	{
		if (isNaN(number) || number=='') number = 0;
		var decimals 		= decimals != undefined ? decimals : 2,
			dec_point		= dec_point != undefined ? dec_point : '.',
			thousands_sep	= thousands_sep != undefined ? thousands_sep : ' ',
			negative		= number<0;
		if (negative) number *= -1;
		var left 	= parseInt(number, 10),
			right	= Math.round(parseFloat(number.toString().replace(/^\d+./, '0.')) * Math.pow(10, decimals));
		left  = left.toString().split('').reverse().join('')
				.match(/\d{3}|\d{1,2}/g)
				.join(thousands_sep)
				.split('').reverse().join('');
		right = (right / Math.pow(10,decimals)).toString().replace(/^\d+./, '').toString();
		if (right.length < decimals) for (var iRight=right.length; iRight < decimals; iRight++) right += '0';
		return (negative?'-':'')+left+dec_point+right;
}
function check(cl){ $("."+cl).attr("checked", "checked"); }
function uncheck(cl){ $("."+cl).attr("checked", ""); }
function send(f)
{
var str = f.options[f.selectedIndex].value;
eval(str);
} 
