
var divIdName;

$(document).ready(function() {


    // VIDEO PLAYER
    $("#TMM4LinkVideo").click(function() {	   
		loadMovie("292368", "videoHolderTMM4");									   
        centerPopup("#TMM4PopupVideo");
        loadPopup("#TMM4PopupVideo");
    });
	
	$("#TM8LinkVideo").click(function() {
		loadMovie("291472", "videoHolderTM8");									   
        centerPopup("#TM8PopupVideo");
        loadPopup("#TM8PopupVideo");
    });

    $("#TM8LinkText").click(function() {	   
		loadMovie("291472", "videoHolderTM8");									   
        centerPopup("#TM8PopupVideo");
        loadPopup("#TM8PopupVideo");
    });

    $("#TMM4CloseWindow").click(function() {
        disablePopup("#TMM4PopupVideo", "videoHolderTMM4");
    });
	
	$("#TM8CloseWindow").click(function() {
        disablePopup("#TM8PopupVideo", "videoHolderTM8");
    });
});

/* ------------ FOR NEWSLETTER POPUP  ------------ */

/*
@Author: Adrian "yEnS" Mato Gondelle
@website: www.yensdesign.com
@email: yensamg@gmail.com
@license: Feel free to use it, but keep this credits please!			
*/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(div) {
	var totalHeight = getPageSizeWithScroll();
    //loads popup only if it is disabled
    if (popupStatus == 0) {
        $("#backgroundPopup").css({ "opacity": "0.7" });
        $("#backgroundPopup").css({ "height": totalHeight[1] });
        $("#backgroundPopup").fadeIn("def");
        $(div).fadeIn("def");
        popupStatus = 1;
    }
}
//disabling popup with jQuery magic!
function disablePopup(div, videoEl) {
		
	var videoContainer = document.getElementById(videoEl);
	var widgetContainer = document.getElementById(divIdName);
	videoContainer.removeChild(widgetContainer);
		
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        $("#backgroundPopup").fadeOut("def");
        $(div).fadeOut("def");
        popupStatus = 0;
    }

}
//centering popup
function centerPopup(div) {
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $(div).height();
    var popupWidth = $(div).width();

    //centering
    $(div).css({ "position": "absolute", "top": getScreenCenterY() - 200, "left": windowWidth / 2 - popupWidth / 2 });

    //only need force for IE6
    $("#backgroundPopup").css({ "height": windowHeight, "width": windowWidth });

}


function loadMovie(widgetID, videoContainer) {

	var videoEl = document.getElementById(videoContainer);
	var newdiv = document.createElement('div');
	divIdName = 'video_' + widgetID;
	newdiv.setAttribute('id',divIdName);
	videoEl.appendChild(newdiv);	
	
	var flashvars = {}; 
	flashvars.affiliateSiteId = "86620";
	flashvars.widgetId = widgetID;
	flashvars.width = "400";
	flashvars.height = "330";
	flashvars.revision = "3";
	var params = {}; 
	params.wmode = "transparent";
	params.quality = "high";
	params.allowFullScreen = "true";
	params.allowScriptAccess = "always";
	swfobject.embedSWF("http://serve.a-widget.com/service/getWidgetSwf.kickAction", divIdName, "400", "330", "9.0.0",true, flashvars,params);
}

function getScreenCenterY() {
	var y = 0;
	y = getScrollOffset()+(getInnerHeight()/2);
	return(y);
}

function getInnerHeight() {
	var y;
	if (self.innerHeight) { // all except Explorer
		y = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		y = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		y = document.body.clientHeight;
	}
	return(y);
}

function getScrollOffset() {
	var y;
	if (self.pageYOffset) { // all except Explorer
		y = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
		y = document.documentElement.scrollTop;
	} else if (document.body) { // all other Explorers
		y = document.body.scrollTop;
	}
	return(y);
}

function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}