
// CHANCE = Percent chance for the popup to occur.
// ONE_TIME = If the percent chance is not made, will the user not be given another chance to get the popup?

// To implement, uncomment any lines after this.

var ENTRY_CHANCE = 12;
var EXIT_CHANCE = 12;
var ONE_TIME = true;
var ENTRY_SURVEY_ID = '65b9g3ab64';
var EXIT_SURVEY_ID = '65b9g3a994';
var SURVEY_URL_PREFIX = 'http://vovici.com/wsb.dll/s/';
var LOCAL_SURVEY_COOKIE_NAME = 'survey_popup_chance';
var FOURQ_SURVEY_COOKIE_NAME = 'IPE6966';

window.addEvent('domready', function()
{
    check_chances();
});

function check_chances()
{
    var c_popup_chance = Cookie.read(LOCAL_SURVEY_COOKIE_NAME);
    var c_4q_popup = Cookie.read(FOURQ_SURVEY_COOKIE_NAME);
    
    if (!c_popup_chance && !c_4q_popup)
    {
        var rand = Math.floor(Math.random() * 100);

        if (rand < ENTRY_CHANCE)
        {
        	create_survey_overlay();
            create_entry_popupbox();
            Cookie.write(LOCAL_SURVEY_COOKIE_NAME, '1', {duration: 30});
            Cookie.write(FOURQ_SURVEY_COOKIE_NAME, FOURQ_SURVEY_COOKIE_NAME, {duration: 30});
        }
        else if (rand < (ENTRY_CHANCE + EXIT_CHANCE)) 
        {
        	create_survey_overlay();
        	create_exit_popupbox();
            Cookie.write(LOCAL_SURVEY_COOKIE_NAME, '1', {duration: 30});
            Cookie.write(FOURQ_SURVEY_COOKIE_NAME, FOURQ_SURVEY_COOKIE_NAME, {duration: 30});
        }
        else if (ONE_TIME)
        {
            Cookie.write(LOCAL_SURVEY_COOKIE_NAME, '0', {duration: 30});
        }
    }
}

function create_entry_popupbox()
{
	create_popupbox('entry');
}

function create_exit_popupbox()
{
	create_popupbox('exit');
}

function create_popupbox(surveyType)
{
    var docWidth = $(document.body).getCoordinates().width;
    var docHeight = $(document.body).getCoordinates().height;
    var pWidth = 344;
    var pHeight = 232;
    var pLeft = (docWidth / 2) - (pWidth / 2);
    var pTop = (docHeight / 2) - (pHeight / 2);
    var contents = '<div style="position:absolute; margin-left: 310px; margin-top: 16px; display: block; width: 12px; height: 12px; cursor: pointer;" onclick="close_all();"></div><div style="text-align:center; margin-top: 110px; color: #ffffff; font-size: 90%">Got a minute? Tell us what you think in our short survey. We would love to hear from you!</div><div style="text-align:center; margin-top:10px;"><a href="javascript: will_participate_' + surveyType +'();"><img class="pngfix" src="/en_US/survey/images/surveyPop_btn_yes.png"/></a> <br/> <br style="line-height:6px;"/> <a href="javascript: close_all();"><img class="pngfix" src="/en_US/survey/images/surveyPop_btn_no.png"/></a></div>';

    var popupbox = new Element('div', {
        'id': 'popupbox',
        'html': contents,
        'styles': {
            'position':'absolute',
            'z-index': '9998',
            'left': pLeft + 'px',
            'top': pTop + 'px',
            'width': pWidth + 'px',
            'height': pHeight + 'px',
            'border': 'none',
            'background-image': 'url("/en_US/survey/images/surveyPop_bg.png")',
            'padding': '0px'
        }
    });
    popupbox.inject(document.body);

    var popupContents = new Element('div', {
        'id': 'popupContents' ,
        'html': contents,
        'styles': {
            'position':'absolute',
            'z-index': '9999',
            'left': pLeft + 'px',
            'top': pTop + 'px',
            'width': pWidth + 'px',
            'height': pHeight + 'px',
            'border': 'none',
            'padding': '0px'
        }
    }).injectAfter('popupbox');
    if(Browser.Engine.trident4){
        fixPNG();
    }    
}

function create_survey_overlay()
{
	
    var docWidth = $(document.body).getCoordinates().width - 75;
    var docHeight = $(document.body).getCoordinates().height - 75;
    var frame_overlay = new Element('iframe', {
        'id': 'frame_overlay',
        'src': '/en_US/survey/blank.html',
        'frameborder': '0',
        'allowTransparency': 'true',
        'scrolling': 'no',
        'styles': {
            'position':'absolute',
            'z-index': '9998',
            'left': '0px',
            'top': '0px',
            'width': docWidth+'px',
            'height': docHeight+'px',
            'background-color': 'transparent',
            'filter': 'chroma(color="#FFFFFF")',
            'padding': '10px'
        }
    });
    
    frame_overlay.inject(document.body);
}

function close_all()
{
    $('frame_overlay').destroy();
    $('popupbox').destroy();
    $('popupContents').destroy();
}

function will_participate_exit()
{
    open_behind(EXIT_SURVEY_ID);
    close_all();
}

function will_participate_entry()
{
    open_survey(SURVEY_URL_PREFIX + ENTRY_SURVEY_ID);
    close_all();
}

function open_behind(surveyID)
{
    var popup = window.open('/en_US/survey/popup_' + surveyID + '.html', 'ExitPopupBehind', 'toolbar=0,menubar=0,scrollbars=1,status=0,resizable=1,width=250,height=80', '');
    popup.blur();
    window.focus();
}

function open_survey(surveyURL)
{
    var popup = window.open(surveyURL, 'PopupBehind', 'toolbar=0,menubar=0,scrollbars=1,status=0,resizable=1,width=810,height=630', '');
}

function fixPNG(){
	$$('img[class=pngfix]').each(function(el){
		var coord = el.getCoordinates();
		el.setStyles({
			width: coord.width,
			height: coord.height,
			filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + el.src + '")'
		});
		el.src = '/en_US/images/x.gif';
	});

	$('popupbox').setStyles({
		'background-image': 'none',
	    'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="/en_US/survey/images/surveyPop_bg.png")'
	});
}
