jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

var addEvents= {
	ready: function(){
		$('.thumbUpImg,.thumbDownImg').bind(
				'click',
				function(){
					ThumbsUpDown($(this).attr('id'));
				}
			);
			
		$('.thumbUpArticleImg,.thumbDownArticleImg').bind(
				'click',
				function(){
					ThumbsArticleUpDown($(this).attr('id'));
				}
			);	
			
			
		$('#searchBox').bind(
			'focus',
			function(){
				if($('#searchBox').val()=="Search for Movies, Actors, etc."){
					$('#searchBox').val("");
					$('#searchBox').css('color', '#000000') ;
				}
			}
		);
		$('#searchBox').bind(
			'blur',
			function(){
				if($('#searchBox').val()==""){
					$('#searchBox').val("Search for Movies, Actors, etc.");
					$('#searchBox').css('color', '#8C8C8C') ;
				}
			}
		);
		
		
		$('#frmSearch').bind(
			'submit',
			function(){
				if($('#searchBox').val()=="" || $('#searchBox').val()=="Search for Movies, Actors, etc."){
					return false;
				}else{
					window.location="search/"+$('#searchBox').val();
					return false;
				}
			}
		);
		
		
		
	}
}

function ThumbsArticleUpDown(thisID){
	cookieName="rated"+thisID;
	if($.cookie(cookieName)!=1){
		$.post("ratearticle.php",{rateID: thisID, articleID: $('#articleID').val()},
	  	function(data){
			rating=data.split("|"); 
			if(rating[2]=="thumbsUp"){
				$("#uprate").text(rating[0]);
				$("#uprate").effect("highlight", {color: "#24365E"}, 2000);
			}else{
				$("#downrate").text(rating[1]);
				$("#downrate").effect("highlight", {color: "#24365E"}, 2000);
			}	
	  	});
		
		$.cookie(cookieName, 1, { expires: 365 });
	}else{
		alert("You may only Thumbs Up and Thumbs Down an article once.")
	}
}

function ThumbsUpDown(thisID){
	cookieName="rated"+thisID+$('#movieID').val();
	if($.cookie(cookieName)!=1){
	$.post("ratereview.php",{rateID: thisID, movieID: $('#movieID').val()},
	  function(data){
		rating=data.split("|"); 
		if(rating[3]=="thumbsUp"){
			$("#uprate"+rating[2]).text(rating[0]);
			$("#uprate"+rating[2]).effect("highlight", {color: "#24365E"}, 2000);
		}else{
			$("#downrate"+rating[2]).text(rating[1]);
			$("#downrate"+rating[2]).effect("highlight", {color: "#24365E"}, 2000);
		}	
	  });

	$.cookie(cookieName, 1, { expires: 365 });
	}else{
		alert("You may only Thumbs Up and Thumbs Down a review once.")
	}
}	

function gup(name){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null){
		return "";
	}else{
		return results[1];
	}
}



$(document).ready(addEvents.ready);





		
		

