


$(function(){

/*** Set main DIVs hight ***/

  $(window).load(function(){
  var MaxHeight = Math.max($('#LeftDiv').height(),$('#RightDiv').height(),$('#CenterDiv').height());
    $('#LeftDiv,#RightDiv,#CenterDiv').css('min-height',MaxHeight); 
    
    }) 

/***EOF***/


/*var border = RUZEE.ShadedBorder.create({ corner:8, shadow:16,  border:2 });
 border.render('siteMenu');*/

  $('ul.mainMenu').css('background-color',$('.mainMenu > li:first').css('background-color'));
    	
            //$(".curved").corner();
			//$("ul.sf-menu > li:last").corner("round 15px tr br");
  

window.sid = 1000288;  






 $('.insertedcss').css('width','auto');
    // $('.curved').cornerz(); 
    if(!$.browser.msie){$('object').append('<param name="wmode" value="transparent">').find('embed').attr('wmode','transparent')};
     if($(".sf-menu").size()){
	$(".sf-menu").superfish();
    var mpos = "1";
    mpos = parseInt(mpos);
    if(mpos == 2 || mpos == 4){
    $("ul.sf-menu").addClass('sf-vertical');
    
    }
}
if($(".sf-menu-custom").size()){
	$(".sf-menu-custom").superfish().find('ul').css({'list-style':'none','position':'absolute','margin':0,'padding-top':0});


}

  
 
if($('#theForm').size()){

        $('input,select,textarea,radio').each(function(i,elm){
			
			//$(this).attr('class','');
			var meta = $(elm).clone().metadata({
				   type: 'attr',
				   name: 'json'
				})
              
				 $(this).attr('name',$(this).parent().parent('tr').find('th').find('label').html());
                 if($(this).attr('name')){
                    if(meta.required!=='0'){
    
                    $(this).addClass('required');
                    }
                    if(meta.rule!=='0'){
    
                    $(this).addClass(meta.rule); 
    
                    }
                    if(meta.rule == 'numberic'){
                    
                    $(this).addClass('number'); 
                    }
                    if(meta.err_msg){
                    
                    $(this).attr('title',meta.err_msg)
                    }
                    if(meta.init_val){
                    
                    $(this).val(meta.init_val)
                    }
                }


		});
		

		var formMeta = $("#theForm").clone().metadata({
				   type: 'attr',
				   name: 'json'
				})
		
                	if(formMeta.font_color){
                  //  alert(formMeta.font_color);
                   $("#theForm").css('color',formMeta.font_color);
                    }
                    if(formMeta.back_color){
                    
                   $("#theForm").css('background-color',formMeta.back_color);
                   
                    }
	$("#theForm").validate({
	submitHandler:function(){
	$('#theForm').ajaxSubmit({
    type:'POST'
    ,url:'/helper/mailformtraget'
    ,success:function(data){alert("Your message has been sent")}
    ,iframe:true
    ,data:{subject:formMeta.email_subject,email_from:formMeta.email_from,email_to:formMeta.email_to}
       }); 
	
	}
	})
    


$('#form1Submit').removeAttr('onclick');

}

if($('.mailForm').size()){
//alert('ahmed');
$form = $(".mailForm");
	$form.validate({
	submitHandler:function(){
     var formMeta = $form.clone().metadata({
				   type: 'attr',
				   name: 'json'
				})
	$form.ajaxSubmit({
    type:'POST'
    ,url:'/helper/mailformtraget'
    ,success:function(data){alert("Your message has been sent")}
    ,iframe:false
    ,data:{subject:formMeta.email_subject,email_from:formMeta.email_from,email_to:formMeta.email_to}
       }); 
	return false; 
	}
	})

/*     $('.mailForm').bind('submit',function(){
    var formMeta = $(this).clone().metadata({
				   type: 'attr',
				   name: 'json'
				})
           $(this).ajaxSubmit({
            type:'POST'
            ,url:'/helper/mailformtraget'
            ,success:function(data){alert("Your message has been sent")}
            ,iframe:false
            ,data:{subject:formMeta.email_subject,email_from:formMeta.email_from,email_to:formMeta.email_to}
             });
                return false; 
        })*/
        
        }
})




/*Rollover by Dapina*/



function imageholderclass(){
	this.over=new Array();
	this.down=new Array();
	this.src=new Array();
	this.store=store;
	
	function store(src, down, over){
		var AL=this.src.length;
		this.src[AL]=new Image(); this.src[AL].src=src;
		this.over[AL]=new Image(); this.over[AL].src=over;
		this.down[AL]=new Image(); this.down[AL].src=down;
	}
}

var ih = new imageholderclass();
var mouseisdown=0;

function preloader(t){
	for(i=0;i<t.length;i++){
		if(t[i].getAttribute('srcover')||t[i].getAttribute('srcdown')){
			
			storeimages(t[i]);
			var checker='';
			checker=(t[i].getAttribute('srcover'))?checker+'A':checker+'';
			checker=(t[i].getAttribute('srcdown'))?checker+'B':checker+'';
			
			switch(checker){
			case 'A' : mouseover(t[i]);mouseout(t[i]); break;
			case 'B' : mousedown(t[i]); mouseup2(t[i]); break;
			case 'AB' : mouseover(t[i]);mouseout(t[i]); mousedown(t[i]); mouseup(t[i]); break;
			default : return;			
			}
			
			if(t[i].src){t[i].setAttribute("oldsrc",t[i].src);}
		}
	}
}
function mouseup(t){
	var newmouseup;
	if(t.onmouseup){
		t.oldmouseup=t.onmouseup;
		newmouseup=function(){mouseisdown=0;this.src=this.getAttribute("srcover");this.oldmouseup();}

	}
	else{newmouseup=function(){mouseisdown=0;this.src=this.getAttribute("srcover");}}
	t.onmouseup=newmouseup;
}

function mouseup2(t){
	var newmouseup;
	if(t.onmouseup){
		t.oldmouseup=t.onmouseup;
		newmouseup=function(){mouseisdown=0;this.src=this.getAttribute("oldsrc");this.oldmouseup();}
		}
	else{newmouseup=function(){mouseisdown=0;this.src=this.getAttribute("oldsrc");}}
	t.onmouseup = newmouseup;
}

function mousedown(t){
	var newmousedown;
	if(t.onmousedown){
		t.oldmousedown=t.onmousedown;
		newmousedown=function(){if(mouseisdown==0){this.src=this.getAttribute("srcdown");this.oldmousedown();}}
	}
	else{newmousedown=function(){if(mouseisdown==0){this.src=this.getAttribute("srcdown");}}}
	t.onmousedown=newmousedown;
}

function mouseover(t){
	var newmouseover;
	if(t.onmouseover){
		t.oldmouseover=t.onmouseover;
		newmouseover=function(){this.src=this.getAttribute("srcover");this.oldmouseover();}
	}
	else{newmouseover=function(){this.src=this.getAttribute("srcover");}}
	t.onmouseover=newmouseover;
}

function mouseout(t){
	var newmouseout;
	if(t.onmouseout){
		t.oldmouseout=t.onmouseout;
		newmouseout=function(){this.src=this.getAttribute("oldsrc");this.oldmouseout();}
	}
	else{newmouseout=function(){this.src=this.getAttribute("oldsrc");}}
	t.onmouseout=newmouseout;
}

function storeimages(t){
	var s=(t.getAttribute('src'))?t.getAttribute('src'):'';
	var d=(t.getAttribute('srcdown'))?t.getAttribute('srcdown'):'';
	var o=(t.getAttribute('srcover'))?t.getAttribute('srcover'):'';
	ih.store(s,d,o);
}

function preloadimgsrc(){
	if(!document.getElementById) return;
	var it=document.getElementsByTagName('IMG');
	var it2=document.getElementsByTagName('INPUT');
	preloader(it);
	preloader(it2);
}

if(window.addEventListener){window.addEventListener("load", preloadimgsrc, false);} 
else{
	if(window.attachEvent){window.attachEvent("onload", preloadimgsrc);}
	else{if(document.getElementById){window.onload=preloadimgsrc;}}
}

function compareSelected(id){
       	$('.compareButtonDiv').show();
		window.compare = String(id);
        window.compareItems = 1;
		$('.adsCompareDiv').each(function (i){
				if($(this).attr('id')!=id){
					$(this).html('<input type="button" value="Add" onclick="addCompare('+$(this).attr('id')+')" />'); 
				}else{
					$(this).html('<input type="button" value="Remove" onclick="removeCompare('+id+')" />'); 
				}
		});
}


function addCompare(id){
		var compare = window.compare;
		compare += "-"+id;
		window.compare = compare;
        window.compareItems++;
		$('.adsCompareDiv').each(function (i){
				if($(this).attr('id')==id){
					$(this).html('<input type="button" value="Remove" onclick="removeCompare('+id+')" />'); 
				}
		});
}


function removeCompare(id){
		var compare = window.compare;
		compare = compare.replace("-"+id,"");
        window.compare = compare;
        window.compareItems--;
        //alert(window.compareItems);
        if(window.compareItems == 0){
          $('.compareButtonDiv').hide();
          window.compare = "";
          $('.adsCompareDiv').each(function (i){
                      $(this).html('<input type="checkbox" value="'+$(this).attr('id')+'" name="compare[]" class="adsSelectCheckbox" onchange="compareSelected('+$(this).attr('id')+')"/>'); 
          });
        }else{
          $('.adsCompareDiv').each(function (i){
                  if($(this).attr('id')==id){
                      $(this).html('<input type="button" value="Add" onclick="addCompare('+$(this).attr('id')+')" />'); 
                  }
          });
        }
		
}

function doCompare(){
		var compare = window.compare;
        //alert(compare);
        var pos = compare.search("-");
        if(pos >= 0){
            window.compare = "";
        	location.href=location.href+"#"+compare;
            getAdDetails(compare, window.detailsStyle);
        }else{
        	alert('You Have to Select 2 Ads or more');
        }
		
}


function pickCompareAd(id){
        	location.href=window.hRef+"#"+id;
            getAdDetails(id, window.detailsStyle);
}

function goToPage(x,div){
	//alert('#ESCAdsResponse'+div);
	$('#ESCAdsResponse'+div).load('/helper/escadspaging/page/'+x)
}

function filterAds(filter,value,div){
	//$('#ESCAdsFiltering').remove();
		var url = '/helper/escadsfilter/'+filter+'/'+value;
	//alert(url);
	
	$('#ESCAdsResponse'+div).load(url,function(data){$('#ESCAdsResponse'+div).removeAttr("style");});
}


function changeView(view,div){
	//$('#ESCAdsFiltering').remove();
		var url = '/helper/changeview/view/'+view;
	//alert(url);
	
	$('#ESCAdsResponse'+div).load(url,function(data){
    
   $('#ESCAdsResponse'+div).removeAttr("style");  
  
    
    });
} 


function getAdDetails(id, style, quantityShow){
	//alert(style);
    if(window.detailsDisplay != "vertical"){
		var menuPos = $("#siteMenu").attr("pos");
		//alert(menuPos);
		if(menuPos == 2){
			//$('#CenterDiv').width($('#CenterDiv').width()+$('#LeftDiv').width()-10);
            var leftWidth = $('#LeftDiv').width();
            var centerWidth = $('#CenterDiv').width();
            centerWidth += leftWidth-4;
			$('#LeftDiv').hide();
			$('#LeftDiv').width('0');
            $('#CenterDiv').width(centerWidth);
		}else if(menuPos == 4){
			//$('#CenterDiv').width($('#CenterDiv').width()+$('#RightDiv').width()-$('#LeftDiv').width());
            var rightWidth = $('#RightDiv').width();
            var centerWidth = $('#CenterDiv').width();
            centerWidth += rightWidth-2;
			$('#RightDiv').hide();
			$('#RightDiv').width('0');
            $('#CenterDiv').width(centerWidth);
		}else{
			$('#LeftDiv').width('0');
			$('#RightDiv').width('0');
			$('#LeftDiv').hide();
			$('#RightDiv').hide();
            $('#CenterDiv').width('100%');
		}
    }
	//$('#CenterDiv').height(0);
        if(id.search("-") >= 0){
        //alert("i'm here");
          $('#CenterDiv').load('/helper/addetails/compare/'+id+'/style/'+style+'/quantityShow/'+quantityShow, null, function(){
               $('#LeftDiv').height($('#CenterDiv').height());
               $(".lightbox").fancybox({'overlayShow':false,'width':570,'height':600,'autoScale':false,'autoDimensions':true,'centerOnScroll':false});
               $(".lightbox2").fancybox({'overlayShow':false,'width':450,'height':300,'autoScale':false,'autoDimensions':true,'centerOnScroll':false});
               window.scrollTo(0,0);
           });
        }else{
          $('#CenterDiv').load('/helper/addetails/adId/'+id+'/style/'+style+'/quantityShow/'+quantityShow+'/view/'+window.view+'/detailsDisplay/'+window.detailsDisplay, null, function(){
               $('#LeftDiv').height($('#CenterDiv').height());
               $(".lightbox").fancybox({'overlayShow':false,'width':570,'height':600,'autoScale':false,'autoDimensions':true,'centerOnScroll':false});
               $(".lightbox2").fancybox({'overlayShow':false,'width':450,'height':300,'autoScale':false,'autoDimensions':true,'centerOnScroll':false});
               window.scrollTo(0,0);
           });
        }
    
}

function enlarge(image,image2,div){
	$("#"+div).html("<img border=\"0\"  src=\""+image+"\"/>");
    $("#"+div).click(function() {
	normalSize(image,image2,div);
});
}

  		
function normalSize(image,image2,div){
	$("#"+div).html("<img border=\"0\"  src=\""+image2+"\"/>");
    $("#"+div).click(function() {
	enlarge(image,image2,div);
});
}


function getRandomAdDetails(id,style){
	//alert(style);
	//$('#LeftDiv').addClass('_closed');
	//$('#RightDiv').addClass('_closed');.width(30)
	$('#LeftDiv').hide();
	$('#RightDiv').hide();
	$('#CenterDiv').width(100+"%");
	$('#CenterDiv').height(0);
	$('#CenterDiv').load('/helper/addetails/adId/'+id+'/style/'+style); 
}

function backTo(url){
//alert(url);
	location.href = url;
}

function loadPhoto(url, div)
{
	url = url.replace("t_","_");
	url = url.replace("t.",".");
    if(window.detailsDisplay == "vertical"){
		$('#'+div).html('<img src="http://www.equipmentsearch.com'+url+'" width="580"  class="thumb" border="0" alt="Listing image" /><br />');
        }else{
	$('#'+div).html('<img src="http://www.equipmentsearch.com'+url+'" width="400"  class="thumb" border="0" alt="Listing image" /><br /><br />');	
        }	
}

$(document).ready(function(){
//$('#LeftDiv').height($('#CenterDiv').height());
//$('#RightDiv').height($('#CenterDiv').height());
if (location.hash) {  
 //alert(location.hash);
         adId = location.hash.replace("#","");
         getAdDetails(adId, window.detailsStyle, window.quantityShow);
         //return ;
         }

var counter = 1;
$('.loanCalc').live("click",function(){
	var prin = 	$("input[name='prin']").val();							 
	var theInt = 	$("input[name='theInt']").val();							 
	var years = 	$("select[name='years']").val();							 
	var howoften = 	$("select[name='howoften']").val();							 
	var show_amort = 	$("input:radio[name='show_amort']:checked").val();							 
	//alert(url);
	if($("input[name='counter']").val() == "first"){
		//alert(counter);
		var menuPos = $("#siteMenu").attr("pos");
		//alert(menuPos);
		if(menuPos == 2){
			//$('#CenterDiv').width($('#CenterDiv').width()+$('#LeftDiv').width()-10);
			$('#LeftDiv').hide();
		}else if(menuPos == 4){
			//$('#CenterDiv').width($('#CenterDiv').width()+$('#RightDiv').width()-$('#LeftDiv').width());
			$('#RightDiv').hide();
		}else{
			$('#LeftDiv').hide();
			$('#RightDiv').hide();
			$('#CenterDiv').width($('#CenterDiv').width()+$('#RightDiv').width()+$('#LeftDiv').width());
		}
}
     var url = '/cgi-bin/calculator.cgi?prin='+prin+'&theInt='+theInt+'&years='+years+'&howoften='+howoften+'&show_amort='+show_amort+'&width='+$('#CenterDiv').width();
	
	//$('.loanCalcContainerDiv').width($('#CenterDiv').width()-10);
	$('.loanCalcResponse').load("/helper/calc", {url:url}, function(responseText, textStatus, XMLHttpRequest){
													//alert($("input:radio[name='show_amort']").val());
				$("select[name='years']").val(years).select();									
				$("select[name='howoften']").val(howoften).select();									
				//$("input:radio").val(show_amort).attr("checked", "checked");	
				$("input:radio[name='show_amort']").each(function() {
				//alert($(this).val());
				if($(this).val() == show_amort) {
					
					$(this).attr("checked", "checked");
				}
				});
													}); 
	
	
});



$('.adDetails').live("click",function(){
	 var adId = $(this).attr('rel');
     location.hash = adId;
     getAdDetails(adId, window.detailsStyle);
});




$('.detailsFormSubmit').live("click",function(){
var id= $(this).parent("td").attr("id");
var captchaVal = $(".captcha"+id).find("input").val();
//alert(captchaVal);
 if($('.detailsFormFirstName'+id).val() != "" && $('.detailsFormLastName'+id).val() != "" && $('.detailsFormPhone'+id).val() != "" && $('.detailsFormEmail'+id).val() != "" && $('.detailsFormComment'+id).val() != "" && captchaVal != ""){
     var url = '/helper/escadscaptcha/captcha/'+captchaVal+'/firstName/'+$('.detailsFormFirstName'+id).val()+'/lastName/'+$('.detailsFormLastName'+id).val()+'/phone/'+$('.detailsFormPhone'+id).val()+'/email/'+$('.detailsFormEmail'+id).val()+'/comment/'+$('.detailsFormComment'+id).val()+'/adId/'+$('.detailsFormAdId'+id).val();
   //  alert(url);
     $('#detailsFormResponseDiv'+id).html("<img src='/images/loader.gif' />");
	 url = url.split(' ').join('%20');
	// alert(url);
     $('#detailsFormResponseDiv'+id).load(url);
	 $('.captcha').load("/helper/createnewcaptcha/");

	// history.go(0);
    
  } else{
   $('#detailsFormResponseDiv'+id).text("all fields are required"); //alert("");
  }
 
 });


$('.financialSubmit').live("click",function(){
var id= $(this).parent("td").attr("id");
var captchaVal = $(".captcha"+id).find("input").val();
//alert(captchaVal);
 if($('.financeFirstName').val() != "" && $('.financeLastName').val() != "" && $('.financePhone').val() != "" && $('.financeEmail').val() != "" && $('.financeComment').val() != "" && captchaVal != ""){
     var url = '/helper/escadscaptcha/captcha/'+captchaVal+'/firstName/'+$('.financeFirstName').val()+'/lastName/'+$('.financeLastName').val()+'/phone/'+$('.financePhone').val()+'/email/'+$('.financeEmail').val()+'/comment/'+$('.financeComment').val()+'/adId/'+$('.financeAdId').val()+'/type/finance';
   //  alert(url);
     $('#financeResponseDiv'+id).html("<img src='/images/loader.gif' />");
	 url = url.split(' ').join('%20');
	// alert(url);
     $('#financeResponseDiv'+id).load(url);
	 $('.captcha').load("/helper/createnewcaptcha/");

	// history.go(0);
    
  } else{
   $('#financeResponseDiv'+id).text("all fields are required"); //alert("");
  }
 
 });



$('.emailSubmit').live("click",function(){
var id= $(this).parent("td").attr("id");
var captchaVal = $(".captcha"+id).find("input").val();
var emailLinkVal = $(".emailLink").val();
//emailLinkVal = emailLinkVal.replace("/","-");
//alert(captchaVal);
 if($('.emailName').val() != "" && $('.emailEmail').val() != "" && captchaVal != ""){
     var url = '/helper/escadscaptcha/captcha/'+captchaVal+'/firstName/'+$('.emailName').val()+'/email/'+$('.emailEmail').val()+'/adId/'+$('.emailAdId').val()+'/emailLink/'+emailLinkVal+'/type/email';
   // alert(url);
     $('#emailResponseDiv'+id).html("<img src='/images/loader.gif' />");
	 url = url.split(' ').join('%20');
	 //alert(url);
     $('#emailResponseDiv'+id).load(url);
	 $('.captcha').load("/helper/createnewcaptcha/");

	// history.go(0);
    
  } else{
   $('#emailResponseDiv'+id).text("all fields are required"); //alert("");
  }
 
 });
 
 
 
});