var state=1;
var th=null;
var sec=5000;

//on every page load
jQuery(document).ready(function(){
        //if not cookie then set it to 1
    if (jQuery.cookie('numPageView')==null) {
        jQuery.cookie('numPageView',1, {expires: 1, path: '/'});
        jQuery.cookie('popupClosed', null);
    } else {
        //else increase it by 1
        jQuery.cookie('numPageView', (jQuery.cookie('numPageView')-0)+1, {path: '/'});
        //alert(jQuery.cookie('numPageView'));
    }

    showbanner();
});

function showbanner() {
    //if view 3 pages then show popup
    if (jQuery.cookie('numPageView')>2) {
        if (jQuery.cookie('popupClosed')!=1){
            //show popup
            setTimeout("fb_show()",1000);
        } else {
            //or if it was closed show docked popup
            dp_show();
        }
    }
}

//show fancybox popup
function fb_show() {
    $.fancybox('',
		{
        		'autoDimensions'	: false,
			'width'        		: 760,
			'height'       		: 470,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
                        'href'                  : '/templates/popup.php',
                        'opacity'               : true,
                        'overlayOpacity'        : 0.5,
                        'overlayColor'          : '#FFF',
                        'hideOnOverlayClick'    : false,
                        'onClosed'              : fb_onclose

		}
	);
}

//on popup close
function fb_onclose() {
    jQuery.cookie('popupClosed', 1, {path: '/'});
    dp_show();
}

//show docked popup
function dp_show() {
    var html='';
    html+='<div id="d_popup" style="background-color: black; position:fixed; bottom:0px; left:50%; width:985px; height:80px; padding: 1px; margin-left: -495px; ;">';
    html+='</div>';
    jQuery(document.body).append(html);
    changeBanners();
}

function changeBanners() {
    jQuery('#d_popup').fadeOut();
    jQuery.ajax({
       url:"/includes/pgtts.showbanners.php",
       success: function(data) {
           //alert(data);
           jQuery('#d_popup').html(data);
           jQuery('#d_popup').fadeIn();
       }
    });
    th=setTimeout('changeBanners();', sec);
}

function toggleButton() {
}

function golink(id, link) {

    var data=jQuery.ajax({url:'/index.php?action=playgame&gameid='+id,async:false}).responseText;

    if (data.substr(0,1)=='{') {
            var dat=eval('('+data+')');
            //console.log('link: '+dat.link+' tab: '+dat.tab+'on: '+this);
            if (dat.tab==0) {
                    //document.location.href=dat.link;
                    $(link).attr('href', dat.link);
                    $(link).attr('target', '_self');
            } else {
                    //var wnd=window.open(dat.link,'_blank');
                    $(link).attr('href', dat.link);
                    $(link).attr('target', '_blank');
            }
			return true;
    } else {
        //link on a game on our site
                $(link).attr('href', '#');
                $(link).attr('target', '');
		document.write(data);
		setTimeout("showbanner();", 2000);
		return false;
		//if view 3 pages then show popup
//        if (jQuery.cookie('numPageView')>2) {
//            if (jQuery.cookie('popupClosed')!=1){
//                //show popup
//                setTimeout("tb_show('','/templates/popup.php?KeepThis=true&TB_iframe=true&height=400&width=600',false)",1000);
//            } else {
//                //or if it was closed show docked popup
//                dp_show();
//            }
//        }
    }
}