var $body = $('body');
var vis_hide = {visibility:"hidden"};
var vis_show = {visibility:"visible"};
var isIE6 = false;
if (window.external && (typeof window.XMLHttpRequest == "undefined")) { isIE6 = true; }

$(document).ready(function(){

	$("#prod_list").change(function() {
                $("#prod_form").load("http://"+window.location.hostname+"/load_form.php?id_p="+$(this).attr("value"));               
		return false;
	});

	$("#show_reg_form").click(function() {
		openPopup($(this).attr("href"),"form");
		return false;
	});

	$("#show_reg_form1").click(function() {
		openPopup($(this).attr("href"),"form");
		return false;
	});

	$("#head_show_mail_form").click(function() {
		openPopup($(this).attr("href"),"form");
		return false;
	});

	$("#show_mail").submit(function() {
		openPopup($(this).attr("action"),"form");
		return false;
	});


	$("#show_job").submit(function() {
		openPopup($(this).attr("action")+'?job_id='+this.job_id.value,"form");
		return false;
	});

	$("#show_order").submit(function() {
		if(this.prod.value == "realty") {
			window.location.href = "http://" + window.location.hostname + "/gk_beaton/expert/trade_estate/";
		} else if(this.prod.value == "offer") {
			window.location.href = "http://" + window.location.hostname + "/gk_beaton/beaton_auto/lease_spec_technic/";
		} else {
			openPopup($(this).attr("action")+"?prod="+this.prod.value,"form");
		}
		return false;
	});
	/*
	$("#show_order").click(function() {
		openPopup($(this).attr("href"),"form");
		return false;
	});
	*/

	
	var decorBg = $(".decor div").css("background-image");

	$(".news-img > a[rel], .reference-wrap .item > a[rel]").bind('hover', function() {

		$rel = this;

		$(".decor div").fadeOut(500, function() {

			var loadImg = new Image();
			loadImg.onload = function() {
				loadImg.onload = null;
				$(".decor div").css({backgroundImage:'url("'+ loadImg.src +'")'}).fadeIn(500);
			};
			loadImg.src = $($rel).attr("rel");
		});

	}).bind('hoverend', function() {

		$(".decor div").fadeOut(500, function() {
			$(this).css({backgroundImage:decorBg}).fadeIn(500);
		});

	});


	$(".signs-links img").bind('hover', function() {

        var curSrc = $(this).attr("src");
        var hoverCurSrc = curSrc.split('.')[0] + "_a.png";
        $(this).attr("src", hoverCurSrc);

    }).bind('hoverend', function() {

        var curSrc =$(this).attr("src");
        var outCurSrc = curSrc.split('_a.png')[0] + ".png";
        $(this).attr("src", outCurSrc);

    });


	$(".modal-link").click(function() {
		
		if (isIE6) {
			$("select").css(vis_hide);
			$body.css({
				height: $body.height()+'px',
				width: $body.width()+'px',
				position: 'static',
				overflow: 'hidden'
			});
			$('html').css({overflow: 'hidden'});
		}

		$(".modal-wrap").show();
		
		return false;
	});

	$(".modal-bg").click(function(){
		$(".modal-wrap").hide();
		if (isIE6) {
			$body.css({height: '', width: '', position: '', overflow: ''});
			$('html').css({overflow: ''});
			$("select").css(vis_show);
		}
	});

	$('#inpText, #inpPass')
		.focus(function() {
			$(this).css({backgroundPosition:'0 -1000px'});
		})
		.blur(function() {
			if(this.value == '') {
				$(this).css({backgroundPosition:'0 0'});
			}
		})
		.focus().blur();


});

function mach_abs()
  {
    $('#res_field').empty();
    var c_area=$('#c-area').val();
    var c_h=$('#c-h').val();
    if (c_area>0 && c_h>0)
      {
         var res=c_area*2.3*(c_h/100);
         $('#res_field').append(res.toFixed(2));
      }
    else $('#res_field').append("0");
  }

function is_in_array(arr,item)
  {
    res=false;
    for(var arr_item in arr)
      if (arr[arr_item]==item)
        {res=true;break;}
    return res;
  }

function GetSelectItem(list_id,is_repeat)
  {
        prod=$("#prod_list").attr("value");
        iten_id=$("#"+list_id).attr("value");  
             
        //alert('http://beaton.kelnik.ru/get_items.php1?prod='+prod+'&t='+(new Date).getTime());
        $.getJSON('http://'+window.location.hostname+'/get_items1.php?prod='+prod+'&t='+(new Date).getTime(), {},
        function(json)
          {
             var items = Array();
             //alert(dump(json, 'json'));
             for(var i in json.items) 
               {   
                  items[i] = Array();            
                  items[i]['sel']=$("#"+i).attr("value");  
                  items[i]['show']=true;                                                  
                  for (j=0;j<json.items[i].length;j++)
                     {   
                       items[i][j] = Array();
                       items[i][j]['show']=true;                    
                       items[i][j]['value']=json.items[i][j].value;
                       items[i][j]['text']=json.items[i][j].text;
                     }                   
               }
               
             for(var from_control in json.ref)
               {
                 for(var to_control in json.ref[from_control])
                    {
                       if (to_control!=list_id){
                       for (i=0;i<items[to_control].length;i++)
                         {
                            var objSel=json.ref[from_control][to_control][items[from_control]['sel']]
                            if (typeof eval(objSel) != "undefined"){
                            if (items[to_control][i]['show']==true) 
                             {                                                                           
                                    items[to_control][i]['show']=is_in_array(json.ref[from_control][to_control][items[from_control]['sel']],items[to_control][i]['value'])                               
                             }}                            
                         }}
                    }

               }
             
             if (json.exception_field!=null)
               { 
                 for(var i in items) 
                   { 
                     var objSel=json.exception_field[items[i]['sel']];
                     for(var j in items)
                       {
                         if (typeof eval(objSel) != "undefined")
                           {                          
                             items[j]['show']=!is_in_array(objSel,j);
                           }
                       }
                   }
               }

             var redraw=false;
             for(var i in items) 
               {                  
                  var opt_str=''; 
                  if (i!=list_id) {$("#"+i).empty();} 
  
                  if (items[i]['show']==true)
                    {  
                      var count_item=0; 
                      for (j=0;j<items[i].length;j++)
                        {   
                         if (i!=list_id)
                           {    
                             if (items[i][j]['show']==true)   
                               {      
                                 if (items[i][j]['value']==items[i]['sel']) 
                                   opt_str='<option value="'+items[i][j]['value']+'" selected>'+items[i][j]['text']+'</option>';                           
                                 else
                                   {
                                     opt_str='<option value="'+items[i][j]['value']+'">'+items[i][j]['text']+'</option>';
                                     //items[i]['sel']=items[i][j]['value'];
                                   }                        
                                 $("#"+i).append(opt_str);
                                 count_item++;
                               }
                           }
                        } 
                      $("#div_"+i).show();                   
                    }
                  else
                    {
                      opt_str='<option value="-1" selected>не заполняется</option>'; 
                      $("#"+i).append(opt_str);count_item++;
                      $("#div_"+i).hide();
                    }
                  if (count_item<1 && i!=list_id && is_repeat!=0) redraw=true;                                     
               }
              //if (redraw==true) GetSelectItem(list_id,0);
          });
  }

function AddStadyPlace(frm_id)
  {
    if(document.getElementById)
      var frm=document.getElementById(frm_id);
    else 
      if(document.all)
        var frm=document.all[frm_id];
      else
        var frm=null; 
     if (frm!=null)
       {
         frm.stady_place_count.value++;
         frm.submit();
       }
  }

function AddWorkPlace(frm_id)
  {
    if(document.getElementById)
      var frm=document.getElementById(frm_id);
    else 
      if(document.all)
        var frm=document.all[frm_id];
      else
        var frm=null; 
     if (frm!=null)
       {
         frm.work_place_count.value++;
         frm.submit();
       }
  }

function dump(obj, obj_name) {
  var result = '';
  for (var i in obj)
    result += obj_name + '.' + i + ' = ' + obj[i] + '\n';

} 

function getFlash (fileId, fileName, fileWidth, fileHeight, noScript) {

	var checkForFlash = function (version){
		var flashIsInstalled = false;
		var flash;
		if(window.ActiveXObject){
			try{
				flash = new ActiveXObject(("ShockwaveFlash.ShockwaveFlash." + version));
				flashIsInstalled = true;
			}
			catch(e){
				// Throws an error if the version isn't available
			}
		}
		else if(navigator.plugins && navigator.mimeTypes.length > 0){
			flash = navigator.plugins["Shockwave Flash"];
			if(flash){
				var flashVersion = navigator.plugins["Shockwave Flash"].description.replace(/.*\s(\d+\.\d+).*/, "$1");
				if(flashVersion >= version){
					flashIsInstalled = true;
				}
			}
		}

		return flashIsInstalled;
	};

	var fstr;


	if(checkForFlash(8)) {
		fstr = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+ fileWidth +'" height="'+ fileHeight +'" id="'+ fileId +'" align="center">'
			+ '<param name="allowScriptAccess" value="sameDomain" />'
			+ '<param name="movie" value="/files/'+ fileName +'.swf" />'
			+ '<param name="quality" value="high" />'
			+ '<param name="wmode" value="transparent" />'
			+ '<embed src="/files/'+ fileName +'.swf" quality="high" width="'+ fileWidth +'" height="'+ fileHeight +'" name="'+ fileId +'" align="center" wmode="transparent" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'
			+ '</object>';
	} else {
		fstr = noScript;
	}

	document.write(fstr);

}

function openPopup(url,name) {
	var popupWin = top[name];
	var pHeight = String(screen.height/2);
	if (popupWin != null && typeof(popupWin) == 'object' && !popupWin.closed) {
		if (popupWin.document.location.href != url) {
			popupWin.document.location.href = url;
		}
	} else {
		popupWin = window.open(url, name, 'width=720,height=' + pHeight + ',status=no,menubar=no,resizable=yes,scrollbars=yes,left=' + String((screen.width-720)/2) + ',top=' + String((screen.height-pHeight)/2));
	}
	popupWin.focus();
}

current_i =2;

function a(i){

	if(current_i){
		obj = document.getElementById("typed"+current_i);
		obj.style.display='none';	
	}
	//alert (i.value);
	current_i = i.value;
	obj = document.getElementById("typed"+current_i);
	obj.style.display='block';
}
