var url = document.location.search;

var ifFirm = $.string(url).include("firm");

function regionOperate(){
      
        
        $("#regionSearchButton").click(function(){
            $("#searchRegion").animate({ height: 'show', opacity: 'show' }, '500');
         });
                 
        $("#searchRegion li").click(function(){
        $("#searchRegion").animate({ height: 'hide', opacity: 'hide' }, '500');
        $myvalue = $(this).text();
        $("#sRegion").val($(this).attr("id"));
        $("#regionButTitle").text($myvalue);
        }); 
        
        $("#searchRegion li").hover(function(){
        $(this).css("background-color", "#FCAF17");
        $(this).css("color", "black");
        });
        $("#searchRegion li").mouseout(function(){
        $(this).css("background-color", "#808184");
        $(this).css("color", "#DADBDC");
        }); 
}


function open_pricelist(){

var tmp_tar1=document.getElementById("main_window_price");
var tmp_tar2=document.getElementById("main_window_all");
var tmp_tar3=document.getElementById("main_window_gallery");

document.getElementById("main_firm_view").style.background="url('../images/grey_bttn.png')";
document.getElementById("firm_price_list").style.background="url('../images/yellow_bttn.png')";
document.getElementById("firm_gallery").style.background="url('../images/grey_bttn.png')";

tmp_tar2.style.height="1px";
tmp_tar3.style.height="1px";
tmp_tar1.style.height="auto";

}


function open_main_view(){

var tmp_tar1=document.getElementById("main_window_all");
var tmp_tar2=document.getElementById("main_window_price");
var tmp_tar3=document.getElementById("main_window_gallery");

document.getElementById("main_firm_view").style.background="url('../images/yellow_bttn.png')";
document.getElementById("firm_price_list").style.background="url('../images/grey_bttn.png')";
document.getElementById("firm_gallery").style.background="url('../images/grey_bttn.png')";

tmp_tar2.style.height="1px";
tmp_tar3.style.height="1px";
tmp_tar1.style.height="auto";
}

function open_main_gallery(){

var tmp_tar1=document.getElementById("main_window_gallery");
var tmp_tar2=document.getElementById("main_window_price");
var tmp_tar3=document.getElementById("main_window_all");

document.getElementById("main_firm_view").style.background="url('../images/grey_bttn.png')";
document.getElementById("firm_price_list").style.background="url('../images/grey_bttn.png')";
document.getElementById("firm_gallery").style.background="url('../images/yellow_bttn.png')";

tmp_tar2.style.height="1px";
tmp_tar3.style.height="1px";
tmp_tar1.style.height="auto";

var i=0;
while (new_target=document.getElementById('gallery_item'+i))
 {new_target.src=new_target.src.replace("images/loading.gif?","");
  new_target.onload=function(){size_to_frame_unique();}
	i++;
 }

}

function size_to_frame_unique()
{var i=0;
while (new_target=document.getElementById('gallery_item'+i))
 {
  if (new_target.height>=new_target.width)
   {new_target.style.height="160px";new_target.style.width="auto";
   } else {new_target.style.height="auto";new_target.style.width="160px";
           new_target.style.marginTop=(new_target.width-new_target.height)/2+"px";
		  }
	i++;
 }

}

function centerPopup(popupWindow){
//request data for centering
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var popupHeight = $(popupWindow).height();
var popupWidth = $(popupWindow).width();
//centering
$(popupWindow).css({
"top": windowHeight/2-popupHeight/2,
"left": windowWidth/2-popupWidth/2
});
//only need force for IE6

/*
 $(popupWindow).css({
 "height": windowHeight
 });
 */

}
var override_left=324;
function showSpecWindow(i){
    
switch(i){
    case "1":
        var iWin = "#AskQuestionBlock";
        break;
    case "2":
        var iWin = "#WriteReviewBlock";
        break;
    case "3":
        var iWin = "#RequestForSearchBlock";
        break;
    case "4":
        var iWin = "#LoginMenu";
        break;                
}    
    
$(document).ready(function(){
$("#AskQuestionBlock, #WriteReviewBlock, #RequestForSearchBlock, #LoginMenu").animate({ opacity: 'hide' }, '100');     
centerPopup(iWin);    
$(iWin).animate({ opacity: 'show' }, '500');    
 });    
}

function closePopup(i){
    
switch(i){
    case "1":
        var iWin = "#AskQuestionBlock";
        break;
    case "2":
        var iWin = "#WriteReviewBlock";
        break;
    case "3":
        var iWin = "#RequestForSearchBlock";
        break;
    case "4":
        var iWin = "#LoginMenu";
        break;                
}    
    
$(document).ready(function(){   
$(iWin).animate({ opacity: 'hide' }, '100');
$(".confirmMsg").html('');    
 });    
}

$(document).ready(function(){
  var AddNewBlockClosed=0;
//alert('I:'+AddNewBlockClosed);  
    
    
    
    
    /*
     choose city according to the specific country BEGIN
     */
      $(".countrySelect").change(function () {
         
                var scatval = $(".countrySelect option:selected").val();
                $.get("/checkFirm.php", { action: "region", region_id: scatval },
                  function(data){
                    $("#regionSelectDiv").html(data);
                 });                
              
        });
        
        
      $(".countrySelect2").change(function () {
         
                var scatval = $(".countrySelect2 option:selected").val();
                $.get("/checkFirm.php", { action: "region", region_id: scatval },
                  function(data){
                    $("#regionSelectDiv2").html(data);
                 });                
             
        });
        
        
      $(".countrySelect3").change(function () {
     
                var scatval = $(".countrySelect3 option:selected").val();
                $.get("checkFirm.php", { action: "region", region_id: scatval },
                  function(data){
                    $("#regionSelectDiv3").html(data);
                 });                
            
        });
    /*
     choose city according to the specific country END
     */    

/*
      $("#categorySearchButton").click(function(){
         $("#searchCategory").animate({ height: 'show', opacity: 'show' }, '500');
          });               

          
          
        
          
                   
      $("#searchCategory li").hover(function(){
         $(this).css("background-color", "#FCAF17");
         $(this).css("color", "black");
         });
      $("#searchCategory li").mouseout(function(){
         $(this).css("background-color", "#808184");
         $(this).css("color", "#DADBDC");
         });
         
      $("#main").click(function(){
         $("#searchCategory").animate({ height: 'hide', opacity: 'hide' }, '500');
         $("#searchSubCategory").animate({ height: 'hide', opacity: 'hide' }, '500');
         $("#searchRegion").animate({ height: 'hide', opacity: 'hide' }, '500'); 
         $("#searchFreeText").animate({ height: 'hide', opacity: 'hide' }, '500'); 
         });
 */
        
/*
         
      $("#searchCategory li").click(function(){
         $("#searchCategory").animate({ height: 'hide', opacity: 'hide' }, '500');
         $butText = $(this).text();
         $myvalue = $(this).attr("id");
         $("#sCategory").val($myvalue);
         $("#categoryButTitle").text($butText);
         $scatval = $(this).attr("id");
         $.get("include/getsubcats.php", { action: "subcats", catid: $scatval },
         function(data){
             $("#searchSubCategory").html(data);
         $("#subcategoryButTitle").text("????????????");
        
         
       $("#subcategorySearchButton").click(function(){
            
                 $("#searchSubCategory").animate({ height: 'show', opacity: 'show' }, '500');
                 
          });
                 
       $("#searchSubCategory li").click(function(){
         $("#searchSubCategory").animate({ height: 'hide', opacity: 'hide' }, '500');
         $subbutText = $(this).text();
         $myvalue = $(this).attr("id");
         $("#sScategory").val($myvalue);
         $("#subcategoryButTitle").text($subbutText);
         });
         
         $("#searchSubCategory li").hover(function(){
         $(this).css("background-color", "#FCAF17");
         $(this).css("color", "black");
         
             // Topic windows is shows up begin
         if(!ifFirm){    
             var parentId = $(this).attr("id");
             var tOffset = $(this).offset();
             var tLeft = (tOffset.left + $(this).outerWidth(true))+"px"; 
             var tTop = tOffset.top+"px";
             $("#searchTopic").css({ 
                 left: tLeft, 
                 top: tTop
         });
           
             
             $.get("include/getsubcats.php", { action: "topics", catid: parentId },
                 function(data){
                     
                     if(data != ''){ 
                         $("#searchTopic").animate({ height: 'show', opacity: 'show' }, '500');
                         $("#searchTopic").html(data);
                         $("#searchTopic li").hover(
                              function () {
                                 $(this).css("background-color", "#FCAF17");
                                 $(this).css("color", "black");
                                 }, 
                              function () {
                                 $(this).css("background-color", "#808184");
                                 $(this).css("color", "#DADBDC");
                                 }
                              );
                              
                          $("#searchTopic li").click(function(){
                             
                             $("#searchSubCategory, #searchTopic").animate({ height: 'hide', opacity: 'hide' }, '500');
                             $("#sTopic").val($(this).attr("id"));
                             $("#subcategoryButTitle").text($(this).text());
                          });      
                     }    
                 });
          }        
             // Topic windows is shows up end
                 
         });
         $("#searchSubCategory li").mouseout(function(){
         $(this).css("background-color", "#808184");
         $(this).css("color", "#DADBDC");
         });            
         
             
             });
         });
 */
        
        
        $("#searchtitles input:radio").change(function () {
            $chooseRegionId = $(this).val();
            $("#regionButTitle").text("Город");
            
            $.get("include/getsubcats.php", { action: "regions", regid: $chooseRegionId },
            function(data){
                $("#searchRegion").html(data);
            
                    regionOperate();
                });           
         

        
        });
        
        
        $("#freeText").keyup(function () {
           $("#searchFreeText").animate({ height: 'show', opacity: 'show',left:override_left+'px',top:200+'px' }, '200'); 
           var searchtext = $(this).val(); 
           if($("#catForFirmFreeSearch").val() == "firm") var category = "firm"; else var category = "articles"; 
            $.get("searchFreeText.php", { searchText: searchtext, category: category },
            function(data){
            
                $("#searchFreeText").html(data);
                
                $("#searchFreeText li").hover(function(){
                    $(this).css("background-color", "#FCAF17");
                    $(this).css("color", "black");
                    
                });
                
                $("#searchFreeText li").mouseout(function(){
                    $(this).css("background-color", "#808184");
                    $(this).css("color", "#DADBDC");
                });
                
                $("#searchFreeText li").click(function(){
                    $("#searchFreeText").animate({ height: 'hide', opacity: 'hide' }, '500');
                });
            });         

            
        });        
        
        
 regionOperate();       
                                     
 });
 
 		/*-----------------------------------------------------------+
		 | addLoadEvent: Add event handler to body when window loads |
		 +-----------------------------------------------------------*/
		function addLoadEvent(func) {
			var oldonload = window.onload;
			
			if (typeof window.onload != "function") {
				window.onload = func;
			} else {
				window.onload = function () {
					oldonload();
					func();
				}
			}
		}
		
		/*------------------------------------+
		 | Functions to run when window loads |
		 +------------------------------------*/
		addLoadEvent(function () {
			initChecklist();
		});
		
		/*----------------------------------------------------------+
		 | initChecklist: Add :hover functionality on labels for IE |
		 +----------------------------------------------------------*/
		function initChecklist() {
			if (document.all && document.getElementById) {
				// Get all unordered lists
				var lists = document.getElementsByTagName("ul");
				
				for (var i = 0; i < lists.length; i++) {
					var theList = lists[i];
					
					// Only work with those having the class "checklist"
					if (theList.className.indexOf("checklist") > -1) {
						var labels = theList.getElementsByTagName("label");
						
						// Assign event handlers to labels within
						for (var j = 0; j < labels.length; j++) {
							var theLabel = labels[j];
							theLabel.onmouseover = function() { this.className += " hover"; };
							theLabel.onmouseout = function() { this.className = this.className.replace(" hover", ""); };
						}
					}
				}
			}
		}

 function addGallery(){
	var canMake=$('#canMake').val();
	if(canMake==1){
		document.forms['addGalleryForm'].submit();
	}else{
		alert('Для того чтобы открыть больше чем одну галерею вам нужно приобрести какой-нибудь пакет.');
	}
 }






function preview_gallery(gallery_id)
{


//		  <div id='firm_gallery_container'>
//   <?=$img['title'];?>
//   <a  href="http://www.tenderuslug.ru/data/<?=$img['picture'];?>" rel="lightbox">
//   <img src='images/loading.gif?data/<?=$img['picture'];?>' id='gallery_item<?=$item_count;?>' class='firm_gallery_img' alt='<?=$img['title'];?>'></a>
//  </div>

}



var resize_trigger=0;
var queue=0;
function open_gallery(id , name)
{   document.getElementById('gallery_frame').src='view_gallery.php?id='+id+"&title="+name;
  document.getElementById('gallery_frame').style.height="730px";
  toggle_gallery('gals_cont');
   toggle_gallery('gallery_frame_div');

}

function calLater(func)
{ 
  window.setTimeout(func,100);
}
function close_gallery()
{   toggle_gallery('gallery_frame_div');
   toggle_gallery('gals_cont');
   
}
function toggle_gallery(itam_id){	
	var target=document.getElementById(itam_id);
	if (target!=undefined)
	{ 
     if (parseFloat(target.offsetHeight)<7)
	{var max_height=target.scrollHeight;
	 resize(target,max_height+1,0);
	} else {resize(target,6,0);}

	}
}

function resize(target,new_height,speed){
if (resize_trigger==0)
 {resize_trigger=1;
  doresize(target,new_height,speed);
 } else {calLater(function(){resize(target,new_height,speed);})}


}

function doresize(target,new_height,speed){
var now_height=parseFloat(target.offsetHeight);
var pre=new_height-now_height;
if (pre*pre>100)
{
  if (navigator.userAgent.search("MSIE")>-1)
  {	if (now_height>new_height)
    {now_height-=16;} else {now_height+=16;};
  } else {
	      if (now_height>new_height)
          {now_height-=17;} else {now_height+=15;};
		  }
   
   target.style.height=now_height+"px";
   window.setTimeout("doresize(document.getElementById('"+target.id+"'),"+new_height+","+speed+")",speed);
} else {target.style.height=new_height+"px";resize_trigger=0;}
}





















