// form controllers
jQuery(document).ready(function(){

	// ss forms objects
	var submitbuttons = "#Form_BoxOffice_action_BoxOffice, #Form_InTheaters_action_InTheaters, #Form_Upcoming_action_Upcoming, #Form_Cities_action_Cities";
	var ssforms = "#Form_BoxOffice, #Form_InTheaters, #Form_Upcoming, #Form_Cities";
	 var dropdowns = "#Form_BoxOffice_TopBox, #Form_InTheaters_New, #Form_Upcoming_Upcoming, #Form_Cities_City";
	// stop the form submition
   	jQuery(ssforms).submit(function(e){
   			return false;
	   	});
   	
	// form button click event
	jQuery("#Form_Cities_action_Cities").click(
  		function (ev) {
	  		// get the city name from the dropdown
	  		var city = jQuery("#Form_Cities_City").val();
	  		
	  		var parameters = "location=0,menubar=0,height=600,width=600,toolbar=1,scrollbars=1,status=0,resizable=1,left=0,screenX=0,top=0,screenY=0";
	  		
	  		// open a new window with the google/movies result
	  		window.open("http://www.google.com/movies?sc=1&near=" + city + ",+bc&rl=1", null, parameters);
  		}
  	);
 
  	// dropdown with movies in theater
  	// attach function to the change event
	jQuery("#Form_BoxOffice_action_BoxOffice").click(
  		function () {
	  		// show loading message
	  		jQuery("#divMovieTitle").html("Loading...");
	  		jQuery("#divMovieReview").html("");
	  		
	  		// get the movie's id
	  		var id = jQuery("#Form_BoxOffice_TopBox").val();
	  		var dropdownid = this.id;
	
			triggerReview(dropdownid, id);
  		}
  	);
 jQuery("#Form_InTheaters_action_InTheaters").click(
    function () {
     // show loading message
     jQuery("#divMovieTitle").html("Loading...");
     jQuery("#divMovieReview").html("");
     
     // get the movie's id
     var id = jQuery("#Form_InTheaters_New").val();
     var dropdownid = this.id;
 
   triggerReview(dropdownid, id);
    }
   );
 
 jQuery("#Form_Upcoming_action_Upcoming").click(
  		function () {
	  		// show loading message
	  		jQuery("#divMovieTitle").html("Loading...");
	  		jQuery("#divMovieReview").html("");
	  		
	  		// get the movie's id
	  		var id = jQuery("#Form_InTheaters_New").val();
	  		var dropdownid = this.id;
	
			triggerReview(dropdownid, id);
  		}
  	);
	jQuery("#Form_Upcoming_action_Upcoming").click(
  		function () {
	  		// show loading message
	  		jQuery("#divMovieTitle").html("Loading...");
	  		jQuery("#divMovieReview").html("");
	  		
	  		// get the movie's id
	  		var id = jQuery("#Form_Upcoming_Upcoming").val();
	  		var dropdownid = this.id;
	
			triggerReview(dropdownid, id);
  		}
  	);
  	
	// adding css classes to the submit buttons
  	jQuery(submitbuttons).addClass('movies-button');
  	
  	jQuery(submitbuttons).css('background','url(/themes/vancouverliving/images/button-search.png)');
 
  	
	jQuery(dropdowns).addClass('movies-dropdowns');
});

function showReview(dropdownid, id)
{
	// ajax call to get the movie review
	jQuery.ajax({
  		type: "POST",
  		url: "MoviesWidget/showreview/",
  		data: "id=" + id + "&dropdownid=" + dropdownid,
  		success: function (msg) {
  			
  			var review = msg.split("|");
  			
  			// show the review
			jQuery("#divMovieTitle").html(review[0]);
			jQuery("#divMovieReview").html(review[1]);
		}
	});  	
}

// forms actions
function triggerReview(dropdownid, movieid)
{
	// reconizing the context
	var context = jQuery("#Form_BoxOffice_Context").val();
	if(context == 'movies')
	{
		// show the movie review
	  	showReview(dropdownid, movieid);
	}
	else
	{
		// go to movie section
		document.location.href = "movies?id=" + movieid + "&dropdownid=" + dropdownid;
	}
}

