var navSearchWidget = {
	handleFocus : function(field) {
		 if (field.value=='Movies, actors, directors') {
		 	field.value='';
		 }
	},
	handleSubmit : function(form) {
		if (form.search.value == "Movies, actors, directors" || form.search.value.length == 0) {
			return false;
		}
	    else {
	    	return true;
		}
	}
};
 
var floatie = {
	show : function(msg) {
		$('dhtmlfloatie').innerHTML = msg;
		$('dhtmlfloatie').style.top = document.documentElement.scrollTop + "px";
		Element.show($('dhtmlfloatie'));
		window.setTimeout("floatie.hide();", 3600);
	},
	
	hide : function() {
		Element.hide($('dhtmlfloatie'));
	}
};

var clicker = {
	counter : 0,
	guard : function() {
		if (this.counter >= 1) {
			alert('Please wait, we\'re currently working on your request.');
			return false;
		}
		this.counter++;
		return true;
	},
	reset : function() {
		this.counter = 0;
	}
};

var Constants = {
	
	THUMBS_UP : 1,
	THUMBS_DOWN : 2,
	THUMBS_NONE : 3
	
};


var User = Class.create();
User.prototype = {
	initialize : function(id) {
		this.id = id;
	}
};

var Movie = Class.create();
Movie.prototype = {
	initialize : function(id) {
		this.id = id;
	}
};

var Rating = Class.create();
Rating.prototype = {
	initialize : function(score) {
		this.score = score;
	}
};

var Score = Class.create();
Score.prototype = {
	initialize : function(id) {
		this.id = id;
	}
};
Score.NONE = new Score(-1);
Score.NOT_INTERESTED = new Score(11);
Score.WANT_TO_SEE = new Score(12);

var Pagination = Class.create();
Pagination.prototype = {
	initialize : function(size, range, url) {
		this.page = 0;
		this.size = size;
		this.range = range;
		this.url  = url;
	},
	isFirstPage : function() {
		if (this.page == this.getFirstPage()) {
			return true;
		} else {
			return false;
		}
	},
	getFirstPage : function() {
		return 1;
	},
	isLastPage : function() {
		if (this.page == this.getLastPage()) {
			return true;
		} else {
			return false;
		}
	},
	getLastPage : function() {
		return Math.ceil(this.size / this.range);
	},
	setPage : function(page) {
		this.page = page;
	},
	getLowerPageBound : function() {
		return (this.page - 1) * this.range;
	},
	getUpperPageBound : function() {
		return Math.min(this.page * this.range, this.size);
	},
	hasPreviousPage : function() {
		return this.page > 1;
	},
	hasNextPage : function() {
		if (this.getUpperPageBound() < this.size) {
			return true;
		} else {
			return false;
		}
	}
};

var LoggerLevels = { DEBUG : 0, INFO : 1, WARN : 2, FATAL : 3 };
var logger = {
	level : LoggerLevels.FATAL,
	console : 'console',
	debug : function(msg) {
		if (LoggerLevels.DEBUG >= this.level) {
			this.log("debug", msg);
		}
	},
	info : function(msg) {
		if (LoggerLevels.INFO >= this.level) {
			this.log("info", msg);
		}
	},
	warn : function(msg) {
		if (LoggerLevels.WARN >= this.level) {
			this.log("warn", msg);
		}
	},
	fatal : function(msg) {
		if (LoggerLevels.FATAL >= this.level) {
			this.log("fatal", msg);
		}
	},
	show : function() {
		Element.show($(this.console));
	},
	hide : function() {
		Element.hide($(this.console));
	},
	log : function(style, msg) {
		div = "<div class="+style+">"+msg+"<//div>";
		new Insertion.Top($(this.console), div);
	},
	report : function(msg) {
		// 1. try to submit via ajax
		//new Ajax.Request('debug.sv', {parameters:params});
		
		// 2. if this fails (use Try.these)...just popup an alert and ask them
		// to copy paste the report into an email client
	}
};

Position.Window = {
    getDeltas: function() {
        var deltaX =  window.pageXOffset
            || document.documentElement.scrollLeft
            || document.body.scrollLeft
            || 0;
        var deltaY =  window.pageYOffset
            || document.documentElement.scrollTop
            || document.body.scrollTop
            || 0;
        return { x : deltaX, y : deltaY };
    },
    size: function() {
        var winWidth, winHeight, d=document;
        if (typeof window.innerWidth!='undefined') {
            winWidth = window.innerWidth;
            winHeight = window.innerHeight;
        } else {
            if (d.documentElement && typeof d.documentElement.clientWidth!='undefined' && d.documentElement.clientWidth!=0) {
                winWidth = d.documentElement.clientWidth
                winHeight = d.documentElement.clientHeight
            } else {
                if (d.body && typeof d.body.clientWidth!='undefined') {
                    winWidth = d.body.clientWidth
                    winHeight = d.body.clientHeight
                }
            }
        }
        return {width : winWidth, height : winHeight};
    }
};

var BrowserManager = {

	isIE:function() {
		var browser = navigator.appName
		if (browser.search(/microsoft/i) >= 0) {
			return true;
		} else {
			return false;
		}
	},
	
	getDocumentHeight:function() {
		var ht=0;
		if(typeof (document.height)=="number"){
			ht=document.height;
		}else{
			return document.body.clientHeight;
		}
		return ht;
	},
	
	getDocumentWidth:function() {
		var ht=0;
		if(typeof (document.width)=="number"){
			ht=document.width;
		}else{
			return document.body.clientWidth;
		}
		return ht;
	}
	
};

var TextUtils = {
	isValidEmail : function(em) {
		var filter= /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
		return ( filter.test( em ) );
	},
	
	hasText : function(text) {
		if (text == null || text == "") {
			return false;
		} else {
			return true;
		}
	}
};

var DefaultTextWidget = Class.create();
DefaultTextWidget.prototype = {
	initialize : function(field, text) {
		this.field = field;
		this.text = text;
		Event.observe($(this.field), 'focus', this.handleFocus.bind(this));
		Event.observe($(this.field), 'blur', this.handleBlur.bind(this));
	},
	handleFocus : function() {
		if ($F(this.field) == this.text) {
			this.field.value = "";
		}
	},
	handleBlur : function() {
		if (!TextUtils.hasText($F(this.field))) {
			this.field.value = this.text;
		}
	}
};
