SportpostControls = function(app){
	var controls = this;
	controls.app = app;
	
	controls.init();
	
	$(document).bind("activateContext", function(event, context){
		controls.fixHtml(context);
		controls.activateContext(context);
	})
	
	return controls;
}

SportpostControls.prototype.init = function(){
	var controls = this;

//	Activate NicEdit textarea editors
	if (typeof(nicEditor) == "function"){
		$("textarea.nicedit").not(".nicedit_activated").each(function(){
			var hidePanel = $(this).hasClass("no-panel");
			var nicedit = new nicEditor({noPanel: hidePanel});
			if (!hidePanel) {
				nicedit.panelInstance(this.id)
			} else {
				nicedit.addInstance(this.id)
			}
		}).addClass("nicedit_activated");	
	}

//	Activate "display more" links
	$("a.display-more").not(".display-more-activated").click(function(){
		var close_div = $(this).parent();
		if (close_div.length && close_div.hasClass("short-description")) {
			var open_div = close_div.siblings("div.full-description");
			close_div.remove();
			if (open_div.length) {
				open_div.css({
					display: "block"
				});
			}
		}
		return false;
	}).addClass("display-more-activated");
}


SportpostControls.prototype.activateContext = function(context){
	var controls = this;
	var context = context || document;

//	Activate custom selectboxes
	if (typeof(jQuery.fn.selectbox) == "function"){
		$("select.std-select", context).not(".selectbox_activated").selectbox().addClass("selectbox_activated");
	}

//	Activate custom file upload inputs
	if (typeof(jQuery.fn.filestyle) == "function"){
		$("input[type=file].std-file", context).not(".filestyle_activated").filestyle({ 
		     image: "/images/img/spacer.gif",
		     imageheight : 33,
		     imagewidth : 77,
		     width : 268
		}).addClass("filestyle_activated");
	}

//	Activate post stars ratings	
	if (typeof(SportpostStarRating) == "function"){
		$("ul.rating-stars", context).not("star_rating_activated").each(function(){
			new SportpostStarRating($(this), controls.app)
		}).addClass("star_rating_activated");
	}


//	Activate custom checkboxes
	if (typeof($.fn.checkbox) == "function"){
		$("input.std-checkbox", context).not(".checkbox_activated").checkbox({
			cls: "std-checkbox",
			empty: "/images/img/empty.png"
		}).addClass("checkbox_activated");
	}
}

SportpostControls.prototype.fixHtml = function(context){
	var disableCorners = false;
	if($.browser.msie && $.browser.version.split(".")[0] < 7) {
		disableCorners = true;
	}
	var webkitAvailable = false;
	try {
		webkitAvailable = (document.body.style.WebkitBorderRadius !== undefined);
	} catch(err) {};
	var mozillaAvailable = false;
	try {
		mozillaAvailable = (document.body.style.MozBorderRadius !== undefined);
		var versionIndex = navigator.userAgent.indexOf('Firefox');
		if (versionIndex >= 0 && parseInt(navigator.userAgent.substring(versionIndex+8)) < 3) mozillaAvailable = false;
	} catch(err) {};
	
	if (!webkitAvailable && !mozillaAvailable && !disableCorners) {
		var context = context || document;
		var cornerDivs = $(".corners", context).not(".corners_fixed").not("h2");
		if (cornerDivs.length) {
//			fix background color for colored pages (sportstars)
			var bodyBgColor = $(document.body).css('backgroundColor');
			if (bodyBgColor == "transparent") bodyBgColor = "#edece8";
			var bgColorStyle = 'style="background-color: ' + bodyBgColor + '"';
			
			var html = {
				top: '<div class="tl"></div><div class="tr"></div>',
				btm: '<div class="bl"></div><div class="br"></div>',
				top_colored: '<div class="tl" ' + bgColorStyle + '></div><div class="tr" ' + bgColorStyle + '></div>',
				btm_colored: '<div class="bl" ' + bgColorStyle + '></div><div class="br" ' + bgColorStyle + '></div>'
			};
//			add image corners to headers
			cornerDivs.each(function(){
				var elem = $(this);
				var topOnly = elem.hasClass("top-corners-only");
				var needsColorFix = true;
//				if (elem.hasClass("light") && !elem.hasClass("comments") && !elem.hasClass("blogs") && !elem.hasClass("videos") && !elem.hasClass("news")) needsColorFix = false;
				if (elem.hasClass("light")) needsColorFix = false;

				if (needsColorFix) {
					$(html.top_colored + (topOnly ? "" : html.btm_colored)).prependTo(elem)
				} else {
					$(html.top + (topOnly ? "" : html.btm)).prependTo(elem)
				}
			}).addClass("corners_fixed");
		}
	}
	
//		add menu hover for IE 6
	if (navigator.userAgent.indexOf('MSIE 6') < 0) {
		return;
	} else {
		$("ul.menu li", context).not("hover_activated").hover(function(){
			$(this).addClass("hover");
		}, function(){
			$(this).removeClass("hover");
		}).addClass("hover_activated")
	}
}


// http://plugins.jquery.com/project/cookie version 1.0
jQuery.cookie=function(d,c,a){if(typeof c!="undefined"){a=a||{};if(c===null){c="";a.expires=-1}var b="";if(a.expires&&(typeof a.expires=="number"||a.expires.toUTCString)){if(typeof a.expires=="number"){b=new Date;b.setTime(b.getTime()+a.expires*24*60*60*1E3)}else b=a.expires;b="; expires="+b.toUTCString()}var e=a.path?"; path="+a.path:"",f=a.domain?"; domain="+a.domain:"";a=a.secure?"; secure":"";document.cookie=[d,"=",encodeURIComponent(c),b,e,f,a].join("")}else{c=null;if(document.cookie&&document.cookie!=
""){a=document.cookie.split(";");for(b=0;b<a.length;b++){e=jQuery.trim(a[b]);if(e.substring(0,d.length+1)==d+"="){c=decodeURIComponent(e.substring(d.length+1));break}}}return c}};