﻿
function AddHours(myDate, hours) {
    return new Date(myDate.getTime() + hours * 60 * 60 * 1000);
}

function updateDivChildren(mydiv) {
	if (typeof mydiv.children == 'function') {							
		mydiv.children().each(function() {
			updateDivChildren($(this));
		});					
	}
	
	var x = "";										
	for(var i = 0; i < mydiv.attr('class').split(" ").length; i++) {
		if (mydiv.attr('class').split(" ")[i].length > 0)
			x += "simplemodal_" + mydiv.attr('class').split(" ")[i] + " ";
	}
	mydiv.toggleClass();
	mydiv.toggleClass(x);				
}

$(document).ready(function() {
    var pageCounter = 0;
    pageCounter = $.cookie('TePapaUnveiledCounter');

    if (pageCounter == null) {
        pageCounter = 1;
        $.cookie('TePapaUnveiledCounter', pageCounter + '', { path: '/', expires: AddHours(new Date(), 1) });
    }
    pageCounter = parseInt(pageCounter);

    if (pageCounter == 1) {
    	pageCounter = pageCounter + 1;
    	$.cookie('TePapaUnveiledCounter', pageCounter + '', { path: '/', expires: AddHours(new Date(), 1) });
    	
        $('#outercontainer').append('<br style="clear:both;" />');

        //popup
        $.fx.speeds._default = 1000;
        var $dialog = $('<div id="pop">stuff</div>')
	    .html('<p>Te Papa needs your help! Answer this short survey and be in to win a $200 voucher</p>')
	    .dialog({
	        autoOpen: false,
	        title: 'Online Survey',
	        hide: 'drop',
	        modal: true,
	        width: 550,
			overlayId: 'simpleOverlay',
			containerId: 'simpleContainer',
			dataId: 'simpleData',
			draggable: false,
			resizable: false,
	        buttons: {
	            "Remind me later": function () {
	                $(this).dialog('close');
	            },
	            "No I'm not interested": function () {
	                $.cookie('TePapaUnveiledCounter', pageCounter + '', { path: '/', expires: AddHours(new Date(), 24 * 365) });
	                $(this).dialog('close');
	            },
	            "Complete the online survey": function () {
	                $.cookie('TePapaUnveiledCounter', pageCounter + '', { path: '/', expires: AddHours(new Date(), 24 * 365) });
	                //window.location = 'http://www.surveymonkey.com/s/CQQ9598';
					window.open('http://www.surveymonkey.com/s/UnveiledWebSurveyReTest');		                
	                $(this).dialog('close');
	            }
	        }
	    });

		updateDivChildren($('#pop').parent());
        $dialog.dialog('open');
        
		window.scrollTo(0,0);                

        //remove line above buttons
        $('.ui-dialog-buttonpane').css('border-width', '0 0 0');
    }
});
