/*
	Sport Business
	18/06/2009
	Patrick Carne
*/

$(document).ready(function() {
	main_menu();
	ajax_tabs();
	uploadify();
	contact_form();
	form_submit();
	google_map();
	//smooth_scroll('our-team-contents-item');
	// breadcrumbs();
	our_team_navigation();
});

/** 
 * Sets up the main menu using superfish and hover intent. 
 */
function main_menu() {

	jQuery("#menu ul").superfish({
		delay: 750,
		animation: {opacity: "show", height: "show"},
		speed: "fast",
		autoArrows: false,
		dropShadows: false
	});	
}

function ajax_tabs() {	
	$("#case_studies").tabs();
}

/**
 * Sets up the request form file upload functionality.
 */
function uploadify() {
	$("#attachment").fileUpload({
		"uploader": "/flash/uploader.swf",
		"script": "/uploadify/upload.php",		
		"folder": "/uploadify/uploads",
		"cancelImg": "/img/cancel.png",
		"onSelect": function(e, queue, file) {
			has_attachment(true);
		},
		"onCancel": function(e, queue, file, response, data) {			
			has_attachment(false);			
		}, 
		"onComplete": function(e, queue, file, response, data) {						
			$("#filename").val(file.name);
			
			// Submit the form via AJAX.		
			ajax_form_submit($("#request_proposal_form")); // @todo: refactor this so that the form is not hard coded like that.
		}
	});	
}

function contact_form() {
	$("#request_proposal").hide();
	
	$("#enquiry_tab").click(function() {
		$("#request_proposal").hide();
		$("#contactformWrapper").show();
		
		$("ul.contact_forms li").removeClass("active");
		$(this).parent().addClass("active");
		
		return false;
	});
	$("#request_tab").click(function() {
		$("#contactformWrapper").hide();
		$("#request_proposal").show();
		
		$("ul.contact_forms li").removeClass("active");
		$(this).parent().addClass("active");
		
		return false;
	});	
}



/** 
 * Keeps track of whether the form has an attachment added.
 */
var __has_attachment = false;
function has_attachment(val) {
	__has_attachment = val;
	
	// Clear the filename if there is no longer an attachment.
	if (__has_attachment == false) {
		$("#filename").val("");
	}
}

/** 
 * This is function wraps the underlying form submit task.
 * If there is an attachment, the attachment is uploaded first.
 * And finally, the form is submitted via ajax_form_submit().
 */
function form_submit() {	
	var form_objects = $("#thecontactform, #request_proposal_form");
	var attachment = $("#attachment");
	
	// On form submit, we upload any attachments and then perform an AJAX form submit.
	form_objects.each(function() {
		var form_object = $(this);
		
		form_object.submit(function() {		
			// if there is an attachment, start the file upload (and it will call the ajax form submit function for us.
			// otherwise, call the ajax form submit function directly.
			if (__has_attachment) {				
				attachment.fileUploadStart();
			}
			else {
				ajax_form_submit($(this));
			}
			
			return false;
		});
	});
}

/** 
 * This submits the form object via ajax.
 */
function ajax_form_submit(form_object) {
	$.ajax({
		type: "POST",
		url: form_object.attr("action"),
		data: form_object.serialize(),
		success: function(result) {	
			has_attachment(false);					
			
			// Fade in the thankyou message.			
			$(form_object).fadeOut("fast", function() {				
				$(form_object).next("div.thankyou").fadeIn("slow");
			});
		}
	});	
	
	return false;
}

/** 
 * Sets up the google map link on the contact page to open in the fancy box.
 */
function google_map() {
	$("#map_link").fancybox({
		 zoomSpeedIn: 500,
		 zoomSpeedOut: 500,
		 overlayShow: true,
		 overlayOpacity: .75,
		 frameWidth: 640,
		 frameHeight: 480,
		 hideOnContentClick: false,
		 callbackOnShow: function() {},
		 callbackOnClose: function() {}
	}); 
}

/** 
 * Uses javascript to correct the breadcrumbs for news and case study pages.
 * (Copies the h2.title text and inserts it in the breadcrumb)
 */
function breadcrumbs() {
	
}

function smooth_scroll(context) {
	if(context == null) {
		var links = $('a[href*=#]');
	} else {
		var links = $('a[class='+context+'][href*=#]');
	}
	links.click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
	  	var $target = $(this.hash);
	    $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
	    if ($target.length) {
	    	var targetOffset = $target.offset().top;
	      $('html,body').animate({scrollTop: targetOffset}, 1000);
	      return false;
	    }
	  }
	});
}

function our_team_navigation() {
	// hide all team members
	$('.team-member').hide();
	
	// on team member name click
	$('.our-team-contents-item').click(function(event) {
		var href = this.href;
		var id = href.substring(href.indexOf('#')+1);
		var target_a = $("a#"+id); 
		var target_div = target_a.next("div.team-member");
		
		if ($("div.team-member:visible").length > 0) {			
			$("div.team-member:visible").fadeOut(function() {
				$(this).hide();
				target_div.fadeIn(function() {
					$(this).show();
				});				
				// performScroll(target_a);
			});
		}		
		else {			
			target_div.fadeIn(function() {
				$(this).show();
			});			
			// performScroll(target_a);
		}		

		return false;
	});
}