var baseurl = "http://www.lendinggrid.com/";
var searchword = "Search Hot in Area";
var freelancesearchword = "e.g. PHP, Customer Service, Blog writer";
var slideTime = 300;
var loading = "I'm on it...";

$(document).ready(function(){
	$(document).pngFix();
	$("input#searchq").zoobeedu(searchword);
	
	$("a.cfa").hover(function(){
		var toshow = $(this).attr("zoo");
		$("div."+toshow).slideDown(300);
	}, function(){
		var toshow = $(this).attr("zoo");
		$("div."+toshow).fadeOut(300);
	});
});


function startBlackBerry(){	$("div.blackberry").fadeIn(300);	}
function endBlackBerry(){	$("div.blackberry").fadeOut(300);	}

$("#searchnow").live("click", function(){
	if($("input#searchq").val()=="" || $("input#searchq").val()==searchword){
		$("input#searchq").focus();
		return false;
	}
});

//Show hide search box
$("a.searchinactive").live("click", function(){
	$("div.hdrsearchform").slideToggle(slideTime);
	return false;
});

//Search Now / Submit search
$("input.submitbtn").live("click", function(){
	if($("input.searchq").val()=="" || $("input.searchq").val()==searchword){
		$("input.searchq").focus();
		return false;
	}	
});




$("a#changelocation").live("click", function(){
	$("div.hiastripcate").slideUp(300);
	$("div.hiastrip").slideToggle(300);
	return false;
});

$("a#changecategory").live("click", function(){
	$("div.hiastrip").slideUp(300);
	$("div.hiastripcate").slideToggle(300);
	return false;
});

$("select.hialocation").live("change", function(){
	var tochange = $(this).attr("name");
	var toloc = $(this).val();	
	switch(tochange){
		case "country":
		$("input#savenewtogo").hide();
		$('select#location_country, select#location_city, select#location_area').attr("disabled", "true");		
		$.post("includes/functions.php", {todo: "changefieldsforlocation", change: tochange, location: toloc}, function(data){						
			$('select#location_city').empty().append(data);
			var tochange = "city";
			var toloc = $("select#location_city").val();
			$.post("includes/functions.php", {todo: "changefieldsforlocation", change: tochange, location: toloc}, function(data){			
			$('select#location_area').empty().append(data);
			$('select#location_country,select#location_city, select#location_area').removeAttr("disabled");
			$("input#savenewtogo").show();
			});			
		});
		break;
		case "city":
		$("input#savenewtogo").hide();
		$('select#location_country, select#location_city, select#location_area').attr("disabled", "true");		
		$.post("includes/functions.php", {todo: "changefieldsforlocation", change: tochange, location: toloc}, function(data){			
			$('select#location_area').empty().append(data);
			$('select#location_country, select#location_city, select#location_area').removeAttr("disabled");
			$("input#savenewtogo").show();
		});
		break;
	}	
});


$("a.downloadfilenow").live("click", function(){
	var fid = $(this).attr("id");
	var type = $(this).attr("zoo");
	$.post("includes/functions.php", {todo: "updatedownloadcount", f: fid, t: type}, function(data){			
			$("span.downloadcount").html(data);
	});	
});



/**New Addition**/
$("a#hideblackberry").live("click", function(){
	$("div.blackberry").fadeOut(300);
	return false;
});

$("a#startbiggallery").live("click", function(){
	$("div.blackberry").fadeIn(300);
	var image = $(this).attr("image");
	$("div.photoframe").html('<img src="'+baseurl+'assets/gallery/'+image+'">');
	return false;
});

$("a#rotategallery").live("click", function(){
	var image = $(this).attr("image");
	$("div.photoframe").html('<img src="'+baseurl+'assets/gallery/'+image+'">');
	return false;
});


$("a#closeyellowbar").live("click", function(){
	$("div.yellowbar").slideUp(200);
	return false;
});







function createUploader(upElement, actionFile, zooExtensions, upType, zDPath, callBackFnc){            

	var uploader = new qq.FileUploader({
    element: document.getElementById(upElement),
    action: actionFile, 
    allowedExtensions: zooExtensions, 
    sizeLimit: 0,
    zupLoadDirectoryPath: zDPath,
    onComplete: function(id, fileName, responseJSON){

    //alert(id);    
    $.post("zElementRename.php", {img: fileName, type: upType}, callBackFnc);       				
    } // on Complete ends here
    
}); //var uploader ends here	       

} // Function Ends here



function createUploaderGallery(upElement, actionFile, zooExtensions, upType, zDPath, callBackFnc){            

	var uploader = new qq.FileUploader({
    element: document.getElementById(upElement),
    action: actionFile, 
    allowedExtensions: zooExtensions, 
    sizeLimit: 0,
    zupLoadDirectoryPath: zDPath,
    onComplete: function(id, fileName, responseJSON){

    //alert(id);    
    $.post("zElementRename.php", {img: fileName, type: upType}, callBackFnc);       				
    } // on Complete ends here
    
}); //var uploader ends here	       

} // Function Ends here

$("select#advert_city").live("change", function(){
	$("select#advert_country, select#advert_city, select#advert_area").attr("disabled", "true");
	var city = $(this).val();
	var country = $("select#advert_country").val();
	$.post("includes/functions.php", {todo: "getAreaLists", m: country, c: city}, function(data){				
		$("select#advert_country, select#advert_city, select#advert_area").removeAttr("disabled");
		$("select#advert_area").empty().append(data);
	});	
	$("ol.advertcitieslist").append('<li city="'+city+'" id="'+city+'">'+city+'&nbsp;&nbsp;&nbsp;<a href="javascript:void(0);" id="removefromcitieslist" city="'+city+'" title="Remove '+city+'">x</a></li>');
	$("ol.advertcitieslist li:first").show();
	return false;
});

$("select#advert_area").live("change", function(){
	var area = $(this).val();
	var city = $("select#advert_city").val();
	$("ol.advertareaslist").append('<li city="'+city+'" area="'+area+'" id="'+area+'">'+city+', '+area+'&nbsp;&nbsp;&nbsp;<a href="javascript:void(0);" id="removefromarealist" area="'+area+'" title="Remove '+area+'">x</a></li>');
	$("ol.advertareaslist li:first").show();
	return false;
});

$("a#removefromarealist").live("click", function(){
	$(this).parent().remove();
	return false;
});



$("select#advert_category").live("change", function(){
	var cate = $(this).val();	
	$("ol.advertcatelist").append('<li cate="'+cate+'" id="'+cate+'">'+cate+'&nbsp;&nbsp;&nbsp;<a href="javascript:void(0);" id="removefromcatelist" cate="'+cate+'" title="Remove '+cate+'">x</a></li>');
	$("ol.advertcatelist li:first").show();
	return false;
});




$("a#removefromcitieslist, a#deletefromgallerylist, a#removefromcatelist").live("click", function(){
	$(this).parent().remove();
});



$("a#switchbigimageeditmode").live("click", function(){	
	$("div.currentbigimage").hide();
	$("div.advertuploaderdiv").show();
	return false;
	//startBlackBerry();	
});


$("a#switchbigimageeditmodeback").live("click", function(){	
	$("div.currentbigimage").show();
	$("div.advertuploaderdiv").hide();
	return false;
	//startBlackBerry();	
});


$("a#saveadvertbigthumbs").live("click", function(){
	var adid = $(this).attr("advert");
	var thumbtype = $(this).attr("thumb");
	var x1 = $('#x1').val();
	var y1 = $('#y1').val();
	var x2 = $('#x2').val();
	var y2 = $('#y2').val();
	var w = $('#w').val();
	var h = $('#h').val();	
	if(x1=="" || y1=="" || x2=="" || y2=="" || w=="" || h==""){
		$("div.yellowbar").slideDown(300);
		$("div.yellowbarmsg").html("You must make a selection first");		
		return false;
	}else{
		$("div.yellowbar").slideDown(300);
		$("div.yellowbarmsg").html(loading);
		$("a#saveadvertbigthumbs").hide();
		$.post("includes/functions.php", {todo: "cropandsaveadvertbigthumbs", advert: adid, xone: x1, yone: y1, xtwo: x2, ytwo: y2, width:w, height:h, ttype: thumbtype}, function(data){
			
				if(data=="itsok"){
					window.location = document.URL;
				}else{
					$("div.yellowbar").slideDown(300);
					$("div.yellowbarmsg").html("An error occured!");
				}
		});
	}
	return false;
});


$("#continuetocheckout").live("click", function(){
								var btitle = $("#advert_title").val();								
								var bigimage = $("#cimgbig").val();
								var offerdetail = $("#advert_offerdetail").val();
								var gallery = [];
								$("ul.hiagallery_list li").each(function(){
									gallery.push($(this).attr("image"));
								});																
								
								var type = $("#adverttype").val();
								var country = $("#advert_country").val();
								var city = $("#advert_city").val();
								var citylist = [];
								$("ol.advertcitieslist li").each(function(){
									citylist.push($(this).attr("city"));
								});								
								var areas = [];
								$("ol.advertareaslist li").each(function(){
									areas.push($(this).attr("area")+"-"+$(this).attr("city"));
								});								
								var name = $("#bname").val();
								var cates = [];
								$("ol.advertcatelist li").each(function(){
									cates.push($(this).attr("cate"));
								});																
								
								var about = $("#babout").val();
								var forsale = $("#bforsale").val();
								var more = $("#bmore").val();
								var number = $("#bnumber").val();
								
								var address = $("#baddress").val();
								var address_city = $("#baddress_city").val();
								var address_state = $("#baddress_state").val();
								var address_zip = $("#baddress_zip").val();
								var address_country = $("#baddress_country").val();
								
								var nearby = $("#bnearby").val();
								var website = $("#bwebsite").val();
								var email = $("#email").val();
								var timings = $("#timing").val();
								var facility = $("#facility").val();
								var card = $('input:radio[name=card]:checked').val();								
								var coupon = $('input:radio[name=coupon]:checked').val();						
								var currentadvert = $("#currentadvertid").val();
								var adverttoken = $("#advertoken").val();
								var cimgmedium = $("#cimgmedium").val();
								var cimgsmall = $('#cimgsmall').val();
								
								$("div.yellowbar").slideUp(300);
								
								
								
								
								if(btitle=="" || btitle==" " || btitle=="Untitled"){
									$("div.yellowbar").slideDown(300);									
									$("div.yellowbarmsg").html("Provide your Advert Title.");
									$("#advert_title").focus();
									return false;
								}else if(bigimage==""){									
									$("div.yellowbar").slideDown(300);									
									$("div.yellowbarmsg").html("Please upload an Image for your Advert.");									
									return false;
								}else if(offerdetail=="" || offerdetail==" "){
									$("div.yellowbar").slideDown(300);									
									$("div.yellowbarmsg").html("Provide your Advert detail.");
									$("#advert_offerdetail").focus();
									return false;
								}else if(type=="cityhot" && areas==""){									
										$("div.yellowbar").slideDown(300);									
										$("div.yellowbarmsg").html("Provide areas where you want to display your Advert.");
										$("#advert_area").focus();
										return false;																			
								}else if(name=="" || name==" "){
									$("div.yellowbar").slideDown(300);									
									$("div.yellowbarmsg").html("Provide your business name.");
									$("#bname").focus();
									return false;
								}else if(cates==""){
									$("div.yellowbar").slideDown(300);									
									$("div.yellowbarmsg").html("Provide category for your Advert.");
									$("#advert_category").focus();
									return false;
								}else if(about=="" || about==" "){
									$("div.yellowbar").slideDown(300);									
									$("div.yellowbarmsg").html("Provide a little overview of your business.");
									$("#babout").focus();
									return false;
								}else if(number=="" || number==" "){
									$("div.yellowbar").slideDown(300);									
									$("div.yellowbarmsg").html("Provide contact number for your business.");
									$("#bnumber").focus();
									return false;
								}else if(address=="" || address==" "){
									$("div.yellowbar").slideDown(300);									
									$("div.yellowbarmsg").html("Provide your business address.");
									$("#baddress").focus();
									return false;
								}else if(address_city=="" || address_city==" "){
									$("div.yellowbar").slideDown(300);									
									$("div.yellowbarmsg").html("Provide your business city.");
									$("#baddress_city").focus();
									return false;
								}else if(address_state=="" || address_state==" "){
									$("div.yellowbar").slideDown(300);									
									$("div.yellowbarmsg").html("Provide your business state.");
									$("#baddress_state").focus();
									return false;
								}else if(address_zip=="" || address_zip==" "){
									$("div.yellowbar").slideDown(300);									
									$("div.yellowbarmsg").html("Provide your business zip.");
									$("#baddress_zip").focus();
									return false;
								}else if(address_country=="" || address_country==" "){
									$("div.yellowbar").slideDown(300);									
									$("div.yellowbarmsg").html("Provide your business country.");
									$("#baddresscountry_country").focus();
									return false;
								}else{
									$("#continuetocheckout").hide();
									$("div.yellowbar").slideDown(300);									
									$("div.yellowbarmsg").html(loading);
									$("div.blackcover").fadeIn(300);
																	 

									$.post("includes/functions.php", {todo: "playwithadvert", playtype: "Draft", cadvert: currentadvert, ctoken: adverttoken, copn: coupon, title: btitle, img: bigimage, detail: offerdetail, images: gallery, btype: type, c: country, bc: city, allcities: citylist, a: areas, n: name, category: cates, abt: about, bforsale: forsale, bmore: more, bnumber: number, baddress: address, baddress_city: address_city,baddress_state: address_state,baddress_zip: address_zip,baddress_country: address_country,bnearby: nearby, bsite: website, bemail: email, btimings: timings, bfacility: facility, bcard: card, imgmedium: cimgmedium, imgsmall: cimgsmall}, function(advertdata){					
																				
										if(advertdata!=0){
											window.location = baseurl+"checkout?id="+advertdata;
										}else{
											$("div.blackcover").fadeOut(300);
											$("#continuetocheckout").show();
											$("div.yellowbar").slideDown(300);									
											$("div.yellowbarmsg").html("An error occured. An team of highly trained monkeys are working on this error...");																						
										}
									});
								}								
								
});


$("#updatemycart").live("click", function(){
	var advert = $(this).attr("advertid");
	var month = $("select#duration").val();
	var coupon = $("#discountcode").val();	
	$("select#duration, #discountcode").attr("disabled", "true");
	$("div.yellowbar").slideDown(300);
	$("div.yellowbarmsg").html(loading);
	$.post("includes/functions.php", {todo: "updatemycart", ad: advert, m:month, c:coupon}, function(advertdata){																								
		
		$("select#duration, #discountcode").removeAttr("disabled");
		window.location = document.URL;
	});
});



$("a#tablink").live("click", function(){
	$("a#tablink").each(function(){
		$(this).removeClass().addClass("tablink_inactive");
	});
	$(this).removeClass().addClass("tablink_active");
	var tohide = $(this).attr("hide");
	var toshow = $(this).attr("toshow");
	$("div.hotinarea_"+tohide+"_sprite").hide();
	$("div.hotinarea_"+toshow+"_sprite").show();	
});
