// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var opened = false;
var whichOpened = null;
var typeOpened = null;


$(document).ready(function(){ 
	initListeners();
	$('#flash_container').flash({
      src: '/flash/cherryblossoms.swf',
      width: 1035,
      height: 375,
      wmode: 'transparent',
      quality: 'high',
      allowScriptAccess: 'sameDomain'
  });
	
	});
	
function initListeners() {
	$('#nav li a.ministers_btn').click(function(){
	getWidget($(this).attr("href"), 350, 350,"ministers");
	$('#nav').addClass("opened");
	$(this).parents("li").toggleClass("selected");
	return false;
	});
	
	$('#nav li a.contact_btn').click(function(){
	getWidget($(this).attr("href"), 500, 500,"contact");
	$('#nav').addClass("opened");
	$(this).parents("li").toggleClass("selected");
	return false;
	});
}

// Clears the selected tab
function clearSelected() {
	$('#nav li.selected').removeClass("selected");
}

function clearOpened() {
	$('#nav.opened').removeClass("opened");
}

// Builds the inner div of the widget
function buildInner(path,innerHeight,type) {
	$("#widget_wrapper").append("<div id='widget_inner' class='"+type+"'></div>");
		ajaxReq(path);
}

// adjusts the size of the widget
function adjust(path,widgetHeight,innerHeight,type) {
	clearSelected();
	$("#widget_inner").fadeOut("fast", function(){
	  $("#widget_inner").removeClass(typeOpened);
	  $("#widget_inner").empty();
  	$('#widget_wrapper').animate({
  	   height: widgetHeight}, 400);
  	$('#widget_inner').animate({ height:innerHeight }, 400, function(){
  	  $("#widget_inner").addClass(type);
  	    $("#widget_inner").fadeIn("fast")
  			ajaxReq(path); 
  			});
	})
	
}

// Widgetize the info
function getWidget(path, widgetHeight, innerHeight, type) {
	if (opened == true) {
		if (whichOpened == path){
		    close(path);
	    } else {
			if (typeOpened == type) {
				ajaxReq(path);
			}	else {
				adjust(path,widgetHeight,innerHeight,type);
			}
		    
	    }
		
	} else {
		$("#container").before("<div id='widget_wrapper'></div>");
		$('#widget_wrapper').animate({
		   height:widgetHeight }, 500, function(){
			buildInner(path,innerHeight,type)
			});
		opened = true;
	}
	typeOpened = type;
}


// AJAX request
function ajaxReq(path) {
	$.ajax({
		type: "GET",
		dataType: "script",
		beforeSend: function(xhr) {	
			$("#widget_inner").addClass("loading");
			$("#widget_inner").empty();
			xhr.setRequestHeader("Accept", "text/javascript");
		},
		url: path,
		error: function() {
			$("#widget_inner").html("<p class='error'>An Error Occured</p>")
		},
		complete: function() {	
			$("#widget_inner").removeClass("loading");
			$("#widget_inner").append("<a href='#' class='close'>Close</a>");
			$(".close").click(function(){
				close();
				});
		}
		
	});
	whichOpened = path;
	return false;
}


// For closing the widget
function close(){
	$("#widget_inner").fadeOut(1000,
	  function(){
  		$('#widget_inner').remove();
  		$('#widget_wrapper').animate({
  		   height: 0}, 500, function(){	
  			$('#widget_wrapper').remove();
  			clearSelected();
  			clearOpened();
	    })
  	})
	opened = false;	
	whichOpened = null;

}

// handles the contact form
function initContactEvents() {
	var options = { type: "GET",
					dataType: "script",
					beforeSend: function(xhr) {	
						xhr.setRequestHeader("Accept", "text/javascript");
						$("#contact_form .button").attr("value","Sending");
					},
					error: function() {
						$(".work_detail").html("<p class='error'>An Error Occured</p>")
						},
					complete: function() {
						$("#contact_form .button").attr("value","Thank You");
						$("#contact_form .button").attr("disabled","true");
						$("#contact_form .text_field").attr("value","");
						$("#contact_form textarea").attr("value","");
						}	
					};
	
	$("#contact_form").validate({
		wrapper: "span",
	  	submitHandler: function(form) {
		  	$(form).ajaxSubmit(options)
		  }
	});
}