$(document).ready(function (){
	
	/* ------------------------------------
	 * replace search feild background text
	 * ------------------------------------ */
		
		// on focus
		$('#search-field').focus(function(){
			$('.overlabel').hide();
		});
		
		// on blur
		$('#search-field').blur(function(){
			var searchValue = $('#search-field').val();
			if (searchValue.length > 1) {
				$('.overlabel').hide();
			}
			else {
				$('.overlabel').show();
			}
		});
		
		// if there is a value in the search field
		$("#password").ready(function(){
			var searchValue = $('#search-field').val();
			if (searchValue.length > 1) { 
				$('.overlabel').hide();
			} else { 
				$('#search-field').show();
			}
		});
		
	/* -----------------------------------------------------
	 * submit the account edit/ submit the media accred form
	 * ----------------------------------------------------- */
		
		if ((document.forms.accountForm) != undefined) {
			function submitThisForm(){
				var thisForm = document.forms.accountForm;
				var fName = thisForm['contact_firstname<cfoutput>#fieldExtension#</cfoutput>'].value;
				var lName = thisForm['contact_lastname<cfoutput>#fieldExtension#</cfoutput>'].value;
				var email = thisForm['contact_email<cfoutput>#fieldExtension#</cfoutput>'].value;
				if (fName.length <= 0 || lName.length <= 0 || email.length <= 0) {
					alert('Please fill out all required fields');
					return false;
				}
				else {
					return true;
				}
			}
		}
		
		if ((document.forms.media_accred) != undefined) {
			function submitThisForm(){
				var thisForm = document.forms.media_accred;
				var fName = thisForm['contact_firstname'].value;
				var lName = thisForm['contact_lastname'].value;
				if (fName.length <= 0 || lName.length <= 0) {
					alert('Please fill out all required fields');
					return false;
				}
				else {
					return true;
				}
			}
		}
		
		
	/* --------------------
	 * call the user rating
	 * -------------------- */		
		
		/* Initialize Plugin */
		$("body").ratingsUtils();

		/* After Cached Page */
		$.ajax({
			type: "GET",
			cache: false,
			url: "../COM/phillycinema/user_ratings.cfc?random=" + (Math.random() * Date.parse(new Date())),
			data: { method: "dspUserRating", id: $.Ratings.clientState.productID },
			complete: function(request) {
				var cacheBypass = request.responseText;
				$(".user-rating").html(cacheBypass);
	
				$.Ratings.initialize();
			}
		});
			
		
});

(function ($) { 

$.fn.ratingsUtils = function() {

	var ClientSide = {
	
		/* Client State Variables */
		clientState : {
			loginState : false,
			userID : null,
			userCont : ".user-rating",
			avgCont : ".average-rating",
			productID : null,
			rater : 1,
			finalRating : 0,
			userSaved : null
		},
	
		/* Class Methods */
		initialize : function () { 
		
			if (typeof(window["globalProductID"]) != "undefined") {
				$.Ratings.clientState.productID = globalProductID;
			}
		
			$.ajax({
				type: "GET",
				url: "../COM/phillycinema/user_ratings.cfc?random=" + (Math.random() * Date.parse(new Date())),
				data: { method: "getLoginStatus" },
				complete: function(request) {
					if(request.responseText=="false") { $.Ratings.clientState.loginState = false; }
					else { 	$.Ratings.clientState.loginState = true; }
					
					$.Ratings.build.UserSet();
					$.Ratings.build.AvgSet();
				} // On Complete
			});
			
		}, // Initialize
	
		build : {
	
			UserSet : function () {
			
			
				if ($.Ratings.clientState.loginState==false) {
					$("label",$.Ratings.clientState.userCont).html("Please log in to rate this item.");
				}
				/* If Logged In */
				else {
	
					/* Step 1 */
					if ($.Ratings.clientState.userSaved <= 0 || !$.Ratings.clientState.userSaved ) {
						$("label",$.Ratings.clientState.userCont).html("Rate this:");
					} else {
						$("label",$.Ratings.clientState.userCont).html("Your rating:");
					}
					
					var set = 1;
					var type = "user";
					var rater = 1;
	
					/* Build Series of Empty Stars */
					for (var i = 1; i <= 8; i++) {
						if (i%2) { starHalf = "lstar"; }
						else { starHalf = "rstar"; }
						if (i < 3) { ender = " Star' />"; }
						else { ender = " Stars' />"; }
						$("span.stars",$.Ratings.clientState.userCont).append("<img border='0' src='/img/rating/" + starHalf + "_" + set + ".gif' class='stars off' name='star" + set + "_" + i + "' alt='" + type + rater + ": star" + set + "_" + i + "' title='" + i/2 + ender);
					}
					
					
					/* Step 2 (handle zero) */				
					$("span.stars",$.Ratings.clientState.userCont).prepend("<img border='0' src='/img/rating/0stars.gif' class='zerostars off' alt='0 stars' title='Cancel rating'>");
					$(".zerostars").hover( function() { $(this).addClass("on"); $(this).removeClass("off"); },
											function() { $(this).addClass("off"); $(this).removeClass("on"); })
									.css("cursor", "pointer")
									.click( function() { $.Ratings.clientState.userSaved = 0; $.Ratings.setUserRating(0); });
	
					$("img[ @alt *= '" + type + rater + ":']", $.Ratings.clientState.userCont).each( function() {
						$(this).css("cursor", "pointer");
						var starAlt = $(this).attr("alt");
						var starBuild = starAlt.substring(4, 5);
						var rating = starAlt.substring((starAlt.length-1), starAlt.length);
	
						$(this).mouseover( function() { $.Ratings.stars.empty(starBuild); $.Ratings.stars.lightUsers(rating); });
						$(this).mouseout( function() { $.Ratings.stars.empty(starBuild); $.Ratings.stars.lightUsers($.Ratings.clientState.userSaved); });
						$(this).click( function() { $.Ratings.clientState.userSaved = rating; $.Ratings.setUserRating(rating); });
	
					});
	
					$.Ratings.getUserRating();
					
				}
				
			}, // UserSet
			
			AvgSet : function () {
	
				$("span", $.Ratings.clientState.avgCont).empty();
	
				var set = 2;
				var type = "avg";
				var rater = 1;
	
				for (var i = 1; i <= 8; i++) {
					if (i%2) { starHalf = "lstar"; }
					else { starHalf = "rstar"; }
					if (i < 3) { ender = " Star' />"; }
					else { ender = " Stars' />"; }
			
					var appendStr = "<img border='0' src='/img/rating/" + starHalf + "_" + set;
					appendStr += ".gif' class='stars off' name='star" + set + "_" + i + "' alt='" + type + rater;
					appendStr += ": star" + set + "_" + i + "' title='" + i/2 + ender;
					
					$("span", $.Ratings.clientState.avgCont).append(appendStr);
					}
				
				$.Ratings.getAvgRating();
				
			} // Initial Avg Set
		
		},
	
		getAvgRating : function() {
			$.ajax({
				type: "GET",
				dataType: "html",
				cache: false,
				url: "../COM/phillycinema/user_ratings.cfc?random=" + (Math.random() * Date.parse(new Date())),
				data: { method: "getAvgRating", id: $.Ratings.clientState.productID },
				complete: function(request) {
					var integer = Math.round(request.responseText);
					$.Ratings.stars.lightAvg(integer);
					}
			});
			
		}, // getAvg

		stars : {
		
			lightAvg : function(rating) {
	
				var set = 2;
				var type = "avg";
				var build = 1;
	
				for (var i = 1; i <= rating; i++) {
					var tempStar = "avg" + build + ": star" + set + "_" + i;
					$("img[ @alt = " + tempStar + " ]").removeClass("off").addClass("on").end();
				}
				
			}, // lightAvgStars
	
			lightUsers : function(rating) {
	
				var set = 1;
				var type = "user";
				var build = 1;
	
				for (var i = 1; i <= rating; i++) {
					if (set == 1) { var tempStar = "user" + build + ": star" + set + "_" + i; }
					else { var tempStar = "avg" + build + ": star" + set + "_" + i; }
					$("img[ @alt = " + tempStar + " ]").removeClass("off").addClass("on").end();
				}
				
			}, // lightUserStars
	
			empty : function(rater) {
	
				$("img[ @alt *= 'user" + rater + "' ]").removeClass("on").addClass("off").end();
	
			} // empty
			
		},

		getUserRating : function() {
			if ($.Ratings.clientState.userSaved != undefined) {
				var userSavedRating = $.Ratings.clientState.userSaved;
				$.Ratings.stars.lightUsers(userSavedRating);
			} else {
				$.ajax({
					type: "GET",
					dataType: "html",
					cache: false,
					url: "../COM/phillycinema/user_ratings.cfc?random=" + (Math.random() * Date.parse(new Date())),
					data: { method: "getUserRating", id: $.Ratings.clientState.productID },
					complete: function(request) {
						var integer = Math.round(request.responseText);
						$.Ratings.clientState.userSaved = integer;
						$.Ratings.stars.lightUsers(integer);
					}
				});
			}
		}, // getUser
	
		setUserRating : function (rated) {
			$("span", $.Ratings.clientState.userCont).html("Saving...");
			var finalRating = rated;
			$.ajax({
				type: "GET",
				cache: false,
				url: "../COM/phillycinema/user_ratings.cfc?random=" + (Math.random() * Date.parse(new Date())),
				data: { method: "setUserRating", id: $.Ratings.clientState.productID, rating: finalRating },
				complete: function() {
					$("span", $.Ratings.clientState.userCont).html("");
					$.Ratings.initialize();
				}
	
			});
		} // setUser
	
	} // End

	$.Ratings = $.extend( true, {}, ClientSide );



	/**********************************************/

	return this.each( function(i) { /* Empty Iteration */}
	
	/**********************************************/

	)};

})(jQuery);

/* -------
 * png fix
 * -------

	var _fixPNG = function() {
	    var $images = $('img[@src*="png"]'), png;
	    if ($images.length > 0) {
	        $images.each( function() {
	                png = $(this).attr("src");
	                $(this).attr("src") = 'PATH TO/spacer.gif';
	                $(this).css({
	                    filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + png + "')"
	                });
	            }
	        );
	    }
	}; */

/* ---------------------------
 * suckerfish dropdown ie6 fix
 * --------------------------- */

	sfHover = function() {
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
	if (window.attachEvent) window.attachEvent("onload", sfHover);
