// Main JS

$(document).ready(function()
{
	if(typeof price == 'undefined') var price = 0;
	
	// Show and scroll to each section
	
	$("#listing_type a").click(function() {
			$("#listing_type a").removeClass("sele");
			$(this).addClass("sele");
			var lst_type = $(this).attr("id");
			if (lst_type != "a") $.scrollTo('#listing', 800);
			$('.info').children().hide();
			
			$("#content").css("height", "1250px");
			
			var listing_selected = lst_type;
			//alert(listing_selected);
			$('#info_' + listing_selected).show();
			$('#price').show();
			
			return false;
			
	});
	
	// Uncheck ALL checkboxes when starting
	if ($('.info').length != 0)
	{
		 $(".info").find("input[type='checkbox']").each(function(){
                $(this).attr('checked', false);
         });
	}
	
	//var clength = $("#info_h input[type=checkbox]:checked").length;
	//alert(clength);
	
	// Disable checkboxes for free listing
	$('#info_h input[type=checkbox]').click(function() {
		var clength = 0;
		clength = $("#info_h input[type=checkbox]:checked").length;
		
		if (clength >= 2) {
			$("#info_h input[type=checkbox]").each(function() {
				
				if($.browser.msie){
					if (!$(this).is(':checked')) {
						$(this).attr("disabled", "disabled");	
					}
				}
				else{
					if (!$(this).is(':checked')) {
						$(this).attr("disabled", "disabled");	
					}
				}
			});
		}
		else {
			$("#info_h input[type=checkbox]").removeAttr("disabled");
		}
	});
	
	// IE form hacking NOT WORKINHG
	$("input[type=checkbox]").click(function(){
        if($.browser.msie){
            $(this).fire("change").blur();
            $(this).fire("change").focus(); 
        }
    }); 
	
	
	// Add or subtract the price meter (this is an indicator and isnt actually used to calculate the payment)
	$(':checkbox').click(function() {
		var section = $(this).parent().parent().parent().attr("id");
		var clength = $("#info_h input[type=checkbox]:checked").length;
		
		if (section == "info_b") { var price_val = 25; } 
		else if (section == "info_c") { var price_val = 50; }
		else if (section == "info_d") { var price_val = 50; }
		else if (section == "info_h") 
		{ 
			if (clength > 2) { var price_val = 25; }
			else { var price_val = 0; }
		}
		
		if ($(this).is(':checked'))
		{
			price = (price + price_val);
		}
		else
		{
			if (clength == 2) { var price_val = 25; }
			price = (price - price_val);
		}
		
		var theprice = price;
		//if (j <= 2 && section == "info_h") theprice = 0;
		//if (theprice <= 0) theprice = 0;
		
		$('.current_price').html(theprice);
	});
	
	// Serialise all of the selected checkboxes to be posted
	$('.cnt').click(function() {
		$.scrollTo('#listing', 800);
		valArrayStandard = $("#info_b input:checked").serializeArray(); 
		valArrayFeatured = $("#info_c input:checked").serializeArray(); 
		valArraySpecial = $("#info_d input:checked").serializeArray(); 
		valArrayFree = $("#info_h input:checked").serializeArray(); 
		
		if (valArrayStandard.length == 0 && valArrayFeatured == 0 && valArraySpecial == 0 && valArrayFree == 0) { alert("Nothing has been selected. \n\nYou must select at least one county or one city or one speciality to continue."); }
		else
		{
				$(this).parent().hide();
				$("#info_f").show();
				return false;
		}
		return false;
	});
	
	// This button is appended so its live
	$('#cnt_back').live('click', function() {
		$.scrollTo('#listing', 800);
		$('#info_g').hide();
		$('#info_f').show();
		return false;
	});
	
	$('#cnt_finish').live('click', function() {
		$('#finish').click();
		return false;
	});
	
	// Finish up the add listing process. POST the form
	$('#finish').click(function() {
		valArrayDetails = $("#info_f input").serializeArray();
		valArrayDetailsText = $("#info_f textarea").serializeArray();
		var thumb = $("#thumb").val();
		var banner = $("#banner").val();
		var image_1 = $("#image_1").val();
		var image_2 = $("#image_2").val();
		var image_3 = $("#image_3").val();
		var image_4 = $("#image_4").val();
		var image_5 = $("#image_5").val();
		
		
		$('#info_f').hide();
		$('#info_g').show();
		$('#info_g').css("display", "block");
		$.scrollTo('#head', 800);
		
		
		$("#info_g").html("");
		
		$.ajax({
			type: "POST",
			url: "lib/listing.php",
			cache: false,
			data: { listing: "verify", standard: valArrayStandard, featured: valArrayFeatured, special: valArraySpecial, free: valArrayFree, details: valArrayDetails, detailsT: valArrayDetailsText, thumb: thumb, banner: banner, image_1: image_1, image_2: image_2, image_3:image_3, image_4: image_4, image_5: image_5 },
			success: function(html){ $("#info_g").html(html);   }
		});
		
		return false;					  
	});
	
	// AJAX Loading
	$("#loading").ajaxStart(function(){
   		$(this).show();
	});
	
	$("#loading").ajaxStop(function(){
  		 $(this).hide();
	}); 
	
	// Text fading on homepage...
	function specialText() {
    jQuery($("#content_spec_left a").children("span")).animate({opacity: 1.0}, {duration: 2000})
        .animate({opacity: 0}, {duration: 2000})
        .animate({opacity: 1.0}, {duration: 2000, complete: specialText})
		}
		
	if ($("#content_spec_left").length != 0)
	{
		specialText();
	}
	
	
	// Uploads - Messy
	if ($("#button1").length != 0)
	{
	var button = $('#button1'), interval;
	new AjaxUpload(button,{
		action: 'handler.php', 
		name: 'banner',
		responseType: false,
		onSubmit : function(file, ext){		
		button.text('Uploading');
		this.disable();
		interval = window.setInterval(function(){
				var text = button.text();
				if (text.length < 13){
					button.text(text + '.');					
				} else {
					button.text('Uploading');				
				}
			}, 200);
		},

		onComplete: function(file, response){ 
			button.text('Upload');
	response = jQuery.trim(response);
						
			window.clearInterval(interval);
			this.enable();
			
			$("#banner").val(response);
			$("#thumb_preview_1").show();	
			$("#thumb_preview_1").html('<img src="img/banners/'+response+'" alt="Banner" />');				
		}
	});
	}
	
	if ($("#button2").length != 0)
	{
	var button = $('#button2'), interval;
	new AjaxUpload(button,{
		action: 'handler.php', 
		name: 'thumb',
		onSubmit : function(file, ext){		
		button.text('Uploading');
		this.disable();
		interval = window.setInterval(function(){
				var text = button.text();
				if (text.length < 13){
					button.text(text + '.');					
				} else {
					button.text('Uploading');				
				}
			}, 200);
		},

		onComplete: function(file, response){ 
			button.text('Upload');
	response = jQuery.trim(response);
						
			window.clearInterval(interval);
			this.enable();
			
			$("#thumb").val(response);
			$("#thumb_preview_2").show();	
			$("#thumb_preview_2").html('<img src="img/gallery/'+response+'" alt="Gallery" />');				
		}
	});
	}
	
	if ($("#button3").length != 0)
	{
	var button = $('#button3'), interval;
	new AjaxUpload(button,{
		action: 'handler.php', 
		name: 'image',
		onSubmit : function(file, ext){		
		button.text('Uploading');
		this.disable();
		interval = window.setInterval(function(){
				var text = button.text();
				if (text.length < 13){
					button.text(text + '.');					
				} else {
					button.text('Uploading');				
				}
			}, 200);
		},

		onComplete: function(file, response){ 
			button.text('Upload');
	response = jQuery.trim(response);
						
			window.clearInterval(interval);
			this.enable();
			
			$("image_1").val(response);
			$("#thumb_preview_3").show();	
			$("#thumb_preview_3").html('<img src="img/gallery/'+response+'" alt="Gallery" />');				
		}
	});
	}
	
	if ($("#button4").length != 0)
	{
	var button = $('#button4'), interval;
	new AjaxUpload(button,{
		action: 'handler.php', 
		name: 'image',
		onSubmit : function(file, ext){		
		button.text('Uploading');
		this.disable();
		interval = window.setInterval(function(){
				var text = button.text();
				if (text.length < 13){
					button.text(text + '.');					
				} else {
					button.text('Uploading');				
				}
			}, 200);
		},

		onComplete: function(file, response){ 
			button.text('Upload');
	response = jQuery.trim(response);
						
			window.clearInterval(interval);
			this.enable();
			
			$("#image_2").val(response);
			$("#thumb_preview_4").show();	
			$("#thumb_preview_4").html('<img src="img/gallery/'+response+'" alt="Gallery" />');				
		}
	});
	}
	
	if ($("#button5").length != 0)
	{
	var button = $('#button5'), interval;
	new AjaxUpload(button,{
		action: 'handler.php', 
		name: 'image',
		onSubmit : function(file, ext){		
		button.text('Uploading');
		this.disable();
		interval = window.setInterval(function(){
				var text = button.text();
				if (text.length < 13){
					button.text(text + '.');					
				} else {
					button.text('Uploading');				
				}
			}, 200);
		},

		onComplete: function(file, response){ 
			button.text('Upload');
	response = jQuery.trim(response);
						
			window.clearInterval(interval);
			this.enable();
			
			$("#image_3").val(response);
			$("#thumb_preview_5").show();	
			$("#thumb_preview_5").html('<img src="img/gallery/'+response+'" alt="Gallery" />');				
		}
	});
	}
	
	if ($("#button6").length != 0)
	{
	var button = $('#button6'), interval;
	new AjaxUpload(button,{
		action: 'handler.php', 
		name: 'image',
		onSubmit : function(file, ext){		
		button.text('Uploading');
		this.disable();
		interval = window.setInterval(function(){
				var text = button.text();
				if (text.length < 13){
					button.text(text + '.');					
				} else {
					button.text('Uploading');				
				}
			}, 200);
		},

		onComplete: function(file, response){ 
			button.text('Upload');
	response = jQuery.trim(response);
						
			window.clearInterval(interval);
			this.enable();
			
			$("#image_4").val(response);
			$("#thumb_preview_6").show();	
			$("#thumb_preview_6").html('<img src="img/gallery/'+response+'" alt="Gallery" />');				
		}
	});
	}
	
	if ($("#button7").length != 0)
	{
	var button = $('#button7'), interval;
	new AjaxUpload(button,{
		action: 'handler.php', 
		name: 'image',
		onSubmit : function(file, ext){		
		button.text('Uploading');
		this.disable();
		interval = window.setInterval(function(){
				var text = button.text();
				if (text.length < 13){
					button.text(text + '.');					
				} else {
					button.text('Uploading');				
				}
			}, 200);
		},

		onComplete: function(file, response){ 
			button.text('Upload');
	response = jQuery.trim(response);
						
			window.clearInterval(interval);
			this.enable();
			
			$("#image_5").val(response);
			$("#thumb_preview_7").show();	
			$("#thumb_preview_7").html('<img src="img/gallery/'+response+'" alt="Gallery" />');				
		}
	});
	}
	jQuery(function($) {
		
		$('.client_gallery').addClass('gallery'); // adds new class name to maintain degradability
		
		$('ul.gallery').galleria({
			history   : true, // activates the history object for bookmarking, back-button etc.
			clickNext : true, // helper for making the image clickable
			insert    : '#main_image', // the containing selector for our main image
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
				
				// fade in the image & caption
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					image.css('display','none').fadeIn(1000);
				}
				caption.css('display','none').fadeIn(1000);
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.5);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
				image.attr('title','Next image >>');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.5';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.5); } // don't fade out if the parent is active
				)
			}
		});
	});
	// Gallery


});

jQuery.fn.extend({
    fire: function(evttype){
        el = this.get(0);
        if (document.createEvent) {
            var evt = document.createEvent('HTMLEvents');
            evt.initEvent(evttype, false, false);
            el.dispatchEvent(evt);
        } else if (document.createEventObject) {
            el.fireEvent('on' + evttype);
        }
        return this;
    }
});
