
jaw = new function () {
	
	/* private variables */
	var queueID = false;
	var loadDelay = 2000; // 2 seconds
	var loadQueue = {};

	/* public functions */
	this.init = new function () {	
	}
	
	this.lightsOn = function () {
		return jaw.screen.lightsOn();
	}
	
	this.lightsOff = function () {
		return jaw.screen.lightsOff();
	}
	
	this.getBaseURL = function () {
        var uri = document.URL;
        uri = uri.replace("http://", "");
        var eles = uri.split("/");
        var final = "http://" + eles[0];
        return final;
	}
	
	/* private functions */
}

jaw.screen = new function () {

	/* public functions */
	this.init = function () {
		Event.observe(window, 'load', jaw.screen.setup);
		Event.observe(window, 'resize', jaw.screen.resize);
	}
	
	this.setup = function () {
		var screenObj = document.createElement('div');
		screenObj.id = 'pageScreen';
		document.body.appendChild(screenObj);
		Element.setStyle(screenObj, {display: 'none'});
		sizeScreen();
	}
	
	this.lightsOn = function () {
		sizeScreen();
		var s = $('pageScreen');
		var mp = $('movieplayer');
		Effect.Fade(s, {duration: 1, from: 0.9, to: 0});
		Element.setStyle(mp, {background: '#E6E6E6'});
	}
	
	this.lightsOff = function () {
		sizeScreen();
		var s = $('pageScreen');
		var mp = $('movieplayer');
		Effect.Appear(s, {duration: 1, from: 0, to: 0.9});
		Element.setStyle(mp, {background: '#191919'});
	}
	
	this.resize = function () {
		sizeScreen();
	}
	
	/* private functions */
	function appendLoad (newMethod) {
		var current = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = newMethod;
		} else {
			window.onload = function () {
				if (current) { current(); }
				newMethod();
			}
		}
	}
	
	function positionPlayer () {
		var m = $('mediaplayer');
		var my = m.offsetTop;
		var mx = (document.documentElement.offsetWidth - 900) / 2;
		if (mx < 0) { mx = 0; }
		Element.setStyle(m, {position: 'absolute', top: my+'px', left: mx+'px', zIndex: 999999});
	}
	
	function sizeScreen () {
		var s = $('pageScreen');
		var w = document.documentElement.offsetWidth;
//		if (w < document.documentElement.scrollWidth) { w = document.documentElement.scrollWidth; }
		var h = document.documentElement.scrollHeight;
		if (h < document.body.scrollHeight) { h = document.body.scrollHeight; }
		if (h < document.documentElement.offsetHeight) { h = document.documentElement.offsetHeight; }
		Element.setStyle(s, {width: w + "px", height: h + "px"});
	}
}

jaw.ads = new function () {

	/* --- private variables ----------------------------------------------- */
	var cc = "US";				// country code for localization
	var mode = "house";			// ad-writing mode: ve, mtv, or house

	var written = false;		// has the VE banner.js been called?
	var hasVideo = false;		// is there a video player on the page?

	var veConfig = {};			// VE config options
	var mtvConfig = {};			// MTV CODA config options

	var houseAds = {
		'160x600': new Array (
			{	img:	'wee-rodeo-160-600.jpg',
				link: 'http://shop.jackassworld.com' },
			{	img:	'april-cooks-160x600.jpg',
				link: 'http://shop.jackassworld.com/detail.php?p=60057=e37127888e55b54427fb5b9f0dee9d5d=All' },
			{	img: 	'evel-160x600.jpg',
				link: 'http://shop.jackassworld.com/detail.php?p=59842=All=88711e1e38f4e19977ae188ce888e042' },
			{	img: 	'25-preston-160x600.jpg',
				link: 'http://shop.jackassworld.com' }
		),
		'300x250': new Array (
			{	img:	'300x250preston-kong.jpg',
				link:	'http://shop.jackassworld.com' },
			{	img:	'300x250wee-bull.jpg',
				link:	'http://shop.jackassworld.com' },
			{	img:	'300x250-april-cooks-buyinshop.jpg',
				link:	'http://shop.jackassworld.com/detail.php?p=60057=e37127888e55b54427fb5b9f0dee9d5d=All' },
			{	img:	'300x250-eveldvd-NOW.jpg',
				link:	'http://shop.jackassworld.com/detail.php?p=59842=All=88711e1e38f4e19977ae188ce888e042' }
		),
		'728x90': new Array (
			{ img: 	'wee-rodeo-728-x-90.jpg',
				link: 'http://shop.jackassworld.com' },
			{	img:  'april-728x90.jpg',
				link:	'http://shop.jackassworld.com/detail.php?p=60057=e37127888e55b54427fb5b9f0dee9d5d=All' },
			{ img:  'evel-728x90.jpg',
				link:	'http://shop.jackassworld.com/detail.php?p=59842=All=88711e1e38f4e19977ae188ce888e042' },
			{ img:  '25-preston-728x90.jpg',
				link: 'http://shop.jackassworld.com' },
			{ img: 	'heavymetalinb.jpg',
				link: 'http://www.heavymetalinbaghdad.com' }
		)
	};

	/* --- public (privileged) functions ----------------------------------- */
	this.init = function () {
		testURL();
		if (cc.toLowerCase() == "us" || cc.toLowerCase() == "ca") {
			switch (mode.toLowerCase()) {
				case "ve":
					initVE();
					break;
				case "mtv":
					initMTV();
					break;
				default:
					initHouse();
					break;
			}
		} else {
			/* international ads should use mtv dart */
			mode = "mtv";
			initMTV();
		}
	}
	
	this.setVEConfig = function (config) {
		veConfig = config;
	}
	
	this.setMTVConfig = function (config) {
		mtvConfig = config;
	}
	
	this.hasVideoPlayer = function () {
		hasVideo = true;
	}
	
	this.getCountry = function () {
		return cc;
	}
	
	this.setCountry = function (input) {
		cc = input;
	}
	
	this.placeAd = function (width, height) {
		switch (mode.toLowerCase()) {
			case "ve":
				placeAdVE(width, height);
				break;
			case "mtv":
				placeAdMTV(width, height);
				break;
			default:
				placeAdHouse(width, height);
				break;
		}
	}
	
	this.getDartState = function () {
		if (cc.toLowerCase() === "au" || cc.toLowerCase() === "gb") {
			return "true";			// must be string as it's appended to a string
		}
		return "false";
	}
	
	

	/* --- private functions ----------------------------------------------- */
	function testURL () {
		var query = String(window.location);
		var s = query.indexOf("?");
		if (s >= 0) {
			query = query.split("?");
			query = query[1];
			var t = query.indexOf("&");
			if (t >= 0) {
				params = query.split("&");
				if (params.length > 0) {
					for (i = 0; i < params.length; i++) {
						testParam(params[i]);
					}
				}
			} else {
				testParam(query);
			}
		}
	}
	
	function testParam (param) {
		var s = param.indexOf("=");
		if (s >= 0) {
			var eles = param.split("=");
			if (eles[0] === "_forceCC") {
				cc = eles[1];
			}
		}
	}
	
	/* video egg calls */
	function initVE () {
		if (written === false) {
			if (veConfig.site !== false && veConfig.publisher !== false && veConfig.application !== false) {
				document.write('<scr' + 'ipt src="http://adcontent.videoegg.com/eap/apps/apps_api/banner.js?rand=' + Math.round(Math.random()*10000000) + '"></scr' + 'ipt>');
				written = true;
			}
		}
	}
	
	function placeAdVE (width, height) {
		fallback = '<img src="http://adcontent.videoegg.com/ads/mtv/jackassworld/default/default_' + width + 'x' + height + '.jpg" width="' + width + '" height="' + height + '">';
		if (hasVideo === false) {
			if (width === 728 && height === 90) {
				placeBannerVE();
			} else {
				placeCompanionVE(width, height, fallback);
			}
		} else {
			placeCompanionVE(width, height, fallback);
		}
	}
	
	function placeBannerVE () {
		if (written === true) {
			VE_embedBanner(veConfig);
		}
	}
	
	function placeCompanionVE (width, height, fallback) {
		VE_initAdManager(veConfig);
		VE_embedCompanionAd(width, height, fallback);
	}

	/* mtv calls */
	function initMTV () {
		com.mtvi.ads.AdManager.setUrl(mtvConfig.url);
		com.mtvi.ads.AdManager.setDartSite(mtvConfig.dartsite);
		com.mtvi.ads.AdManager.setZone(mtvConfig.zone);
		com.mtvi.ads.AdManager.setMedia(mtvConfig.media);
		// overwriting the CODA propensity to postpend .mtvi
		com.mtvi.ads.AdManager.DART_SITE_DOMAIN = '.com';
	}
	
	function placeAdMTV (width, height) {
		com.mtvi.ads.AdManager.placeNewAd(width, height);
	}
	
	/* house calls */
	function initHouse () {
	}
	
	function placeAdHouse (width, height) {
		var k = width + "x" + height;
		var source = houseAds[k];
		var i = Math.round(Math.random() * (source.length - 1));
		document.write('<a href="' + source[i].link + '" target="_blank"><img src="' + jaw.getBaseURL() + '/images/house_ads/' + source[i].img + '" width="' + width + '" height="' + height + '" /></a>');
	}

}

jaw.flags = new function () {

	/* public functions */
	this.writeFlags = function (activeLang)
	{
		this.writeFlag("en_uk", "gb", true, activeLang);
		this.writeFlag("it", "it", true, activeLang);
		this.writeFlag("es", "es", true, activeLang);
		this.writeFlag("en_au", "au", true, activeLang);
		this.writeFlag("nl", "nl", true, activeLang);
		this.writeFlag("pt", "pt", true, activeLang);
		this.writeFlag("pt_br", "br", true, activeLang);
		this.writeFlag("de", "de", true, activeLang);
		this.writeFlag("fr", "fr", true, activeLang);
		this.writeFlag("se", "se", true, activeLang);
		this.writeFlag("no", "no", true, activeLang);
		this.writeFlag("dk", "dk", true, activeLang);
		this.writeFlag("fi", "fi", true, activeLang);
		this.writeFlag("ja", "jp", true, activeLang);
		this.writeFlag("es_mx", "mx", false, activeLang);
	}
	this.writeCommunityFlags = function (activeLang)
	{
		this.writeCommunityFlag("en_uk", "gb", true, activeLang);
		this.writeCommunityFlag("it", "it", true, activeLang);
		this.writeCommunityFlag("es", "es", true, activeLang);
		this.writeCommunityFlag("en_au", "au", true, activeLang);
		this.writeCommunityFlag("nl", "nl", true, activeLang);
		this.writeCommunityFlag("pt", "pt", true, activeLang);
		this.writeCommunityFlag("pt_br", "br", true, activeLang);
		this.writeCommunityFlag("de", "de", true, activeLang);
		this.writeCommunityFlag("fr", "fr", true, activeLang);
		this.writeCommunityFlag("se", "se", true, activeLang);
		this.writeCommunityFlag("no", "no", true, activeLang);
		this.writeCommunityFlag("dk", "dk", true, activeLang);
		this.writeCommunityFlag("fi", "fi", true, activeLang);
		this.writeCommunityFlag("ja", "jp", true, activeLang);
		this.writeCommunityFlag("es_mx", "mx", false, activeLang);
	}
	/* private functions */
	this.writeFlag = function (lang, altText, available, activeLang)
	{
		var imageName = "/images/flags/" + lang;

		if (lang == activeLang)
			imageName = imageName + ".jpg";
		else
			imageName = imageName + "_normal.jpg";

		if (available)
			document.write('<a href="/lang/' + lang + '/videos">');

		document.write('<img border="0" src="' + imageName + '" alt="' + altText + '"/>&nbsp;');

		if (available)
			document.write('</a>');
	}
	this.writeCommunityFlag = function (lang, altText, available, activeLang)
	{
		var imageName = "/images/flags/" + lang;

		if (lang == activeLang)
			imageName = imageName + ".jpg";
		else
			imageName = imageName + "_normal.jpg";

		if (available)
			document.write('<a href="http://www.jackassworld.com/lang/' + lang + '/videos">');

		document.write('<img border="0" src="http://www.jackassworld.com' + imageName + '" alt="' + altText + '"/>&nbsp;');

		if (available)
			document.write('</a>');
	}

}

jaw.exclusives = new function () {
	
	this.process = function (code, lang) {
		$('error').style.display = "none";
		$('success').style.display = "none";
		var url = "http://www.jackassworld.com/tools/exclusives.php";
		var config = {
			method: 'post',
			parameters: {
				code: code,
				lang: lang
			},
			evalJS: true,
			onSuccess: jaw.exclusives.receipt
		}
		new Ajax.Request(url, config);
	}
	
	this.keypress = function (event, code, lang) {
		if (event.keyCode == 13) {
			jaw.exclusives.process(code, lang);
		} else {
			return;
		}
	}
	
	this.receipt = function () {
		
	}
	
	this.displayError = function () {
		$('error').style.display = "block";
	}
	
	this.displayVideo = function () {
		$('success').style.display = "block";
		$('error').style.display = "none";
		$('form').style.display = "none";
		$('videocontainer').style.display = "block";
	}
	
}


jaw.ads.setCountry('US');

jaw.ads.setVEConfig({
		site: "jackassworld",
		publisher: "mtv",
		application: "jackassworld",
		alternate: "http://adcontent.videoegg.com/ads/mtv/jackassworld/default/default_600x70.swf"
});
jaw.ads.setMTVConfig({
		url: "/jackassworld.com/",
		dartsite: "jackassworld",
		zone: "jackassworld.com",
		media: "adj"
});
