﻿/***************************/
//@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;
var buttonId;
var backgroundId;
var popupId;
var closeId;
var coupon = new Array();
var divCoupon;
var lnkPrevious;
var lnkNext;
var index = 0;

function setId(btnId,bckId,pId,cId)
{
    buttonId=btnId;
    backgroundId=bckId;
    popupId=pId;
    closeId = cId;
    centerPopup();
    loadPopup();
}

function setValue(cp,divc,lnkp,lnkn)
{
    var sindex = 0;
    var eindex = 0;
    var hidden = document.getElementById(cp);
    var value=hidden.value;
    var piece;
    var i = 0;
    
    lnkPrevious = document.getElementById(lnkp);
    lnkNext= document.getElementById(lnkn);
    divCoupon = document.getElementById(divc);
    coupon=new Array();
    while (value.length > (eindex + 3))
    {
        eindex = value.indexOf("$|$", eindex + i);
        piece = value.substring(sindex, eindex);
        coupon[i] = piece;
        sindex = eindex+3;
        i++;
    }
    index = 0;

    lnkPrevious.style.visibility="hidden";
    divCoupon.innerHTML ="<table style='width:404px;' border=0 cellspacing=0 cellingpadding=0><tr><td style='background:url(../images/topborder.png) no-repeat;width:404px;height:26px'></td></tr><tr><td style='background:url(../images/middle1.png) repeat-y;width:404px'>" + coupon[0] + "</td></tr><tr><td style='background:url(../images/bottomcouponborder.png) no-repeat;width:404px;height:26px'></td></tr></table>";
    if (coupon.length > 1) {
        divCoupon.innerHTML +="<table style='width:404px;' border=0 cellspacing=0 cellingpadding=0><tr><td style='background:url(../images/topborder.png) no-repeat;width:404px;height:26px'></td></tr><tr><td style='background:url(../images/middle1.png) repeat-y;width:404px'>" + coupon[1] + "</td></tr><tr><td style='background:url(../images/bottomcouponborder.png) no-repeat;width:404px;height:26px'></td></tr></table>";
    }
    
    if((coupon.length)<=2) {
        lnkNext.style.visibility="hidden";
    }
    else
    {
        lnkNext.style.visibility="visible";
    }
}

function onClickNext() {
    if (index + 1 < coupon.length - 1) {
        index += 2;
        $(lnkPrevious).css({
            "visibility": "visible"
        });
    }
    if(coupon.length<=index+2){
        $(lnkNext).css({
            "visibility": "hidden"
        });
    }
    divCoupon.innerHTML = "<table style='width:404px' border=0 cellspacing=0 cellingpadding=0><tr><td style='background:url(../images/topborder.png) no-repeat;width:404px;height:26px'></td></tr><tr><td style='background:url(../images/middle1.png) repeat-y;width:404px'>" + coupon[index] + "</td></tr><tr><td style='background:url(../images/bottomcouponborder.png) no-repeat;width:404px;height:26px'></td></tr></table>";
    if (coupon.length > index + 1) {
        divCoupon.innerHTML += "<table style='width:404px' border=0 cellspacing=0 cellingpadding=0><tr><td style='background:url(../images/topborder.png) no-repeat;width:404px;height:26px'></td></tr><tr><td style='background:url(../images/middle1.png) repeat-y;width:404px'>" + coupon[index+1] + "</td></tr><tr><td style='background:url(../images/bottomcouponborder.png) no-repeat;width:404px;height:26px'></td></tr></table>";
    }
}

function onClickPrevious() {
    if (index >= 2) {
        index -= 2;
        $(lnkNext).css({
            "visibility": "visible"
        });
    }
    if (index-1<=0) {
        $(lnkPrevious).css({
            "visibility": "hidden"
        });
    }
    divCoupon.innerHTML = "<table style='width:404px' border=0 cellspacing=0 cellingpadding=0><tr><td style='background:url(../images/topborder.png) no-repeat;width:404px;height:26px'></td></tr><tr><td style='background:url(../images/middle1.png) repeat-y;width:404px'>" + coupon[index] + "</td></tr><tr><td style='background:url(../images/bottomcouponborder.png) no-repeat;width:404px;height:26px'></td></tr></table>";
    if (coupon.length > index + 1) {
        divCoupon.innerHTML += "<table style='width:404px' border=0 cellspacing=0 cellingpadding=0><tr><td style='background:url(../images/topborder.png) no-repeat;width:404px;height:26px'></td></tr><tr><td style='background:url(../images/middle1.png) repeat-y;width:404px'>" + coupon[index+1] + "</td></tr><tr><td style='background:url(../images/bottomcouponborder.png) no-repeat;width:404px;height:26px'></td></tr></table>";
    }
    else {
       
    }
}

//loading popup with jQuery magic!
function loadPopup(){
    var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	
	var popupHeight = $(popupId).height();
	var popupWidth = $(popupId).width();

	//loads popup only if it is disabled
	if(popupStatus==0){
		$(backgroundId).css({
			"opacity": "0"
		});
		$(backgroundId).fadeIn("fast");
		$(popupId).fadeIn("fast");
		//$(popupId).css({width:"0px",height:"0px"});
		//$(popupId).animate({opacity: "1", height: popupHeight, width: popupWidth}, "slow");

		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
    var popupHeight = $(popupId).height();
	var popupWidth = $(popupId).width();
	//disables popup only if it is enabled
	if(popupStatus==1)
	{
		$(backgroundId).fadeOut("fast");
		$(popupId).fadeOut("fast");
		//$(popupId).animate({height: 0, width: 0,"display":"none"}, "slow");
		//$(popupId).css({position:"absolute"});
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
    
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	
	var popupHeight = $(popupId).height();
	var popupWidth = $(popupId).width();
	//alert("pheight :"+ popupHeight + ", pwidth" + popupWidth + ", top:" +windowHeight/2-popupHeight/2);
	//centering
	$(popupId).css({position: "fixed",width:"450px",top: windowHeight/2-popupHeight/2,left: (windowWidth/2)-(popupWidth/2)});
	$(backgroundId).css({height: windowHeight});
}

