IWPopup = function(popupElem, openElem, closeElem){
	//SETTING UP OUR POPUP
	//0 means disabled; 1 means enabled;
	this.isPopupOpen = false;
	var that = this;
	this.popupElem = popupElem;
	this.openElem = openElem;
	this.closeElem = closeElem;
	this.backgroundElem = $("#backgroundPopup");
	this.backgroundElem.css({"opacity": "0.7" }); 
	//LOADING POPUP
	//Click the button event!
	openElem.click(function(){
		//centering with css
		that.centerPopup();
		//load popup
		that.loadPopup();
	});
	//CLOSING POPUP
	//Click the x event!
	closeElem.click(function(){
		that.disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		that.disablePopup();
	}); 
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && that.isPopupOpen){
			that.disablePopup();
		}
	});
};

//loading popup with jQuery magic!
IWPopup.prototype.loadPopup = function(){
	//loads popup only if it is disabled	
	if(this.isPopupOpen === false){
		this.popupElem.css({
			"opacity": "1"
		});
		this.popupElem.fadeIn("slow");
		this.backgroundElem && this.backgroundElem.fadeIn("slow");
		this.isPopupOpen = true;
	}
};

//disabling popup with jQuery magic!
IWPopup.prototype.disablePopup = function(){
	//disables popup only if it is enabled
	if(this.isPopupOpen){
		this.popupElem.fadeOut("slow");
		this.backgroundElem && this.backgroundElem.fadeOut("slow");
		this.isPopupOpen = false;
	}
}

//centering popup
IWPopup.prototype.centerPopup = function(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $(this.popupElem).height();
	var popupWidth = $(this.popupElem).width();
	var scrollTop = $(document).scrollTop();
	//centering
	this.popupElem.css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2 + scrollTop,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	this.popupElem.css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	new IWPopup($("#popupSlenderShaperVideo"), $("#buttonSlenderShaper"), $("#popupSlenderShaperClose"));
	new IWPopup($("#popupRioVideo"), $("#buttonRio"), $("#popupRioClose"));
	new IWPopup($("#popupKymaroVideo"), $("#buttonKymaro"), $("#popupKymaroClose"));
	new IWPopup($("#popupDermaVideo"), $("#buttonDerma"), $("#popupDermaClose"));
	new IWPopup($("#popupRaycopVideo"), $("#buttonRaycop"), $("#popupRaycopClose"));
	new IWPopup($("#popupEcoSteamVideo"), $("#buttonEcoSteam"), $("#popupEcoSteamClose"));
	new IWPopup($("#popupExtremeVideo"), $("#buttonExtreme"), $("#popupExtremeClose"));
	new IWPopup($("#popupNuwaveVideo"), $("#buttonNuwave"), $("#popupNuwaveClose"));
	new IWPopup($("#popupRioNeckVideo"), $("#buttonRioNeck"), $("#popupRioNeckClose"));
	new IWPopup($("#popupBarefootVideo"), $("#buttonBarefoot"), $("#popupBarefootClose"));
	new IWPopup($("#popupBallBikeVideo"), $("#buttonBallBike"), $("#popupBallBikeClose"));
	new IWPopup($("#popupSpinVideo"), $("#buttonSpin"), $("#popupSpinClose"));
	new IWPopup($("#popupBabyVideo"), $("#buttonBaby"), $("#popupBabyClose"));
	new IWPopup($("#popupLaurenVideo"), $("#buttonLauren"), $("#popupLaurenClose"));
	new IWPopup($("#popupLoadVideo"), $("#buttonLoad"), $("#popupLoadClose"));
	new IWPopup($("#popupBraineticsVideo"), $("#buttonBrainetics"), $("#popupBraineticsClose"));
	new IWPopup($("#popupEnergymVideo"), $("#buttonEnergym"), $("#popupEnergymClose"));
	new IWPopup($("#popupKymaroVideo"), $("#buttonKymaro"), $("#popupKymaroClose"));
	new IWPopup($("#popupNoWetVideo"), $("#buttonNoWet"), $("#popupNoWetClose"));
	new IWPopup($("#popupZapVideo"), $("#buttonZap"), $("#popupZapClose"));
	new IWPopup($("#popupDidiVideo"), $("#buttonDidi"), $("#popupDidiClose"));
	new IWPopup($("#popupDuzzitVideo"), $("#buttonDuzzit"), $("#popupDuzzitClose"));
	new IWPopup($("#popupJohnnyVideo"), $("#buttonJohnny"), $("#popupJohnnyClose"));
	new IWPopup($("#popupPancakeVideo"), $("#buttonPancake"), $("#popupPancakeClose"));
	new IWPopup($("#popupRhythmRockerVideo"), $("#buttonRhythmRocker"), $("#popupRhythmRockerClose"));
	new IWPopup($("#popupYBCRGlobalVideo"), $("#buttonYBCRGlobal"), $("#popupYBCRGlobalClose"));
	new IWPopup($("#popupPaintZoomVideo"), $("#buttonPaintZoom"), $("#popupPaintZoomClose"));
	new IWPopup($("#popupAbCircleVideo"), $("#buttonAbCircle"), $("#popupAbCircleClose"));
	new IWPopup($("#popupTowerVideo"), $("#buttonTower"), $("#popupTowerClose"));
	new IWPopup($("#popupShakeVideo"), $("#buttonShake"), $("#popupShakeClose"));
		

});
