var modalVideoCID = false;
var modalVideoVID = false;
var modalFlashSWF = false;
var modalFlashVars = false;
var modalFlashWidth = false;
var modalFlashHeight = false;

function modalLinkOpt() {
	return {
		speed:0,
		scrolling:false,
		close:'',
		onOpen:modalFix,
		onComplete:modalOpen,
		onCleanup:modalCleanup
	};
}

$(function() {
	$('a.modal-link').colorbox(modalLinkOpt());
	$('a.modal-inline').click(function(e) {
		e.preventDefault();
		modalInline($(this).attr('href'));
	});
	$('a.modal-map').click(modalMap);
	$('a.modal-video').click(function(e) {
		e.preventDefault();
		var url = $(this).attr('href');
		modalVideo(getUrlParam('cId', url), getUrlParam('vId', url), getUrlParam('vHeight', url, 430));
	});
	$('a.modal-linkx').click(function(e) {
		e.preventDefault();
		var url = $(this).attr('href');
		modalFlash(url, getUrlParam('width', url, 512), getUrlParam('height', url, 430), getUrlParam('swf', url), getUrlVars(url));
	});
});

// Functions to trigger modal dialogs without links.

function modalLink(href, rel)
{
	var opt = modalLinkOpt();
	opt.href = href;
	opt.rel = rel;
	$.colorbox(opt);
}

function modalInline(href)
{
	var opt = modalLinkOpt();
	opt.inline = true;
	opt.href = href;
	$.colorbox(opt);
}

function modalMap(e)
{
	if (e) e.preventDefault();
	var opt = modalLinkOpt();
	opt.href = '/map/map.html';
	opt.innerWidth = 640;
	opt.innerHeight = 520;
	opt.onComplete = modalMapOpen;
	$.colorbox(opt);
}

function modalVideo(cid, vid, height)
{
	if (height == undefined)
	{
		height = 430;
	}
	modalVideoCID = cid;
	modalVideoVID = vid;
	modalFlashHeight = height;
	var opt = modalLinkOpt();
	opt.href = '/misc/videowrapper/swf.html';
	opt.innerWidth = 512;
	opt.innerHeight = height;
	opt.onComplete = modalVideoOpen;
	opt.onCleanup = modalVideoClose;
	$.colorbox(opt);
}

function modalFlash(url, width, height, swf, flashvars)
{
	var opt = modalLinkOpt();
	opt.href = url;
	opt.innerWidth = width;
	opt.innerHeight = height;
	if (swf != undefined && swf && swf != '')
	{
		modalFlashSWF = swf;
		modalFlashWidth = width;
		modalFlashHeight = height;
		if (flashvars != undefined)
		{
			modalFlashVars = flashvars;
		}
		else
		{
			modalFlashVars = {};
		}
		opt.onComplete = modalFlashOpen;
	}
	$.colorbox(opt);
}

// Adjusting the modal dialog.

function modalFix()
{
	// Stop any js sliders.
	$("#nSlider-container").trigger('nSliderStop');

	$('#cboxWrapper').css('visibility', 'hidden');
	if ($('#cboxClose > #cboxMiddleRight').length > 0)
	{
		return;
	}
	var closebtn = $('#cboxClose');
	closebtn.appendTo($('#cboxMiddleRight'));
	closebtn.attr('display:block');
}

function modalOpen()
{
	$('#cboxWrapper').css('visibility', 'visible');
	if (typeof(DD_belatedPNG) == 'object')
	{
		DD_belatedPNG.fix('#cboxTopLeft, #cboxTopCenter, #cboxTopRight, #cboxBottomLeft, #cboxBottomCenter, #cboxBottomRight, #cboxMiddleLeft, #cboxMiddleRight');
	}
}

function modalCleanup()
{
	// Start any js sliders.
	$("#nSlider-container").trigger('nSliderStart');
	
	$('#cboxWrapper').css('visibility', 'hidden');
	$("#modal-content").remove();
}

// Creating flash elements.

function modalMapOpen()
{
	modalOpen();
	var flashvars_modal = {};
	var params_modal = {};
	params_modal.base = "/map/";
	params_modal.play = "true";
	params_modal.loop = "false";
	params_modal.quality = "high";
	params_modal.bgcolor = "#ffffff";
	params_modal.allowscriptaccess = "sameDomain";
	params_modal.allowfullscreen = "false";
	params_modal.wmode = "opaque";
	var attributes_modal = {};
	swfobject.embedSWF("/map/map5.swf", "map-container", "640", "447", "9.0.45", "/map/expressInstall.swf", flashvars_modal, params_modal, attributes_modal);
}

function modalVideoOpen()
{
	modalOpen();
	var flashvars_modal = {};
	flashvars_modal.catalogId = modalVideoCID;
	flashvars_modal.videoId = modalVideoVID;
	flashvars_modal.autoPlay = "false";
	flashvars_modal.autoRewind = "false";
	flashvars_modal.showPreview = "true";
	flashvars_modal.localeChain = "en_US";
	flashvars_modal.remoteUrl = "http://www.goodmoodtv.com/internettv/application/itella";
	var params_modal = {};
	params_modal.play = "true";
	params_modal.loop = "false";
	params_modal.quality = "high";
	params_modal.bgcolor = "#000000";
	params_modal.allowscriptaccess = "always";
	params_modal.allowfullscreen = "true";
	params_modal.wmode = "transparent";
	params_modal.base = "http://www.goodmoodtv.com/internettv/application/itella/system/";    
	var attributes_modal = {};
	attributes_modal.id = "InternetTV";
	attributes_modal.name = "InternetTV";
	swfobject.embedSWF("http://www.goodmoodtv.com/internettv/application/itella/system/SingleVideoPlayer.swf", "flashdiv", "512", modalFlashHeight, "9.0.45", "http://www.goodmoodtv.com/internettv/application/itella/system/playerProductInstall.swf", flashvars_modal, params_modal, attributes_modal);
}

function modalVideoClose()
{
	var itv = document.getElementById("InternetTV");
	if (itv && typeof(itv.closePlayer) == 'function')
	{
		itv.closePlayer();
	}
	modalCleanup();
}

function removePlayer()
{
	swfobject.removeSWF("InternetTV");
	$.fn.colorbox.close();
}

function modalFlashOpen()
{
	modalOpen();
	if (modalFlashSWF != undefined && modalFlashSWF != '')
	{
		var flashvars_modal = modalFlashVars;
		var params_modal = {};
		params_modal.play = "true";
		params_modal.loop = "false";
		params_modal.quality = "high";
		params_modal.bgcolor = "#000000";
		params_modal.allowscriptaccess = "always";
		params_modal.allowfullscreen = "true";
		params_modal.wmode = "transparent";
		var attributes_modal = {};
		swfobject.embedSWF(unescape(modalFlashSWF), "flashdiv", modalFlashWidth, modalFlashHeight, "9.0.45", "/map/expressInstall.swf", flashvars_modal, params_modal, attributes_modal);
	}
}

// Parses parameter by name from url.

function getUrlParam(name, url, def)
{
	if (def == undefined)
	{
		def = "";
	}
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( url );
	if (results == undefined)
	{
		return def;
	}
	return results[1];
}

function getUrlVars(url)
{
    var vars = {};
    var hashes = url.slice(url.indexOf('?') + 1).split('&');
	var hash = false;
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars[hash[0]] = hash[1];
    }
    return vars;
}

