/**
 * Popup opener helper
 *
 * @author      Thomas Parisot <thomas.parisot@ext.cdiscount.com>
 * @version     1.1
 * @deprecated  Will be replaced by jQuery.popup
 * @param       strUrl  {string}
 * @param       strWindowName   {string}
 * @param       width   {?number}
 * @param       height  {?number}
 * @param       screenx {?number}
 * @param       screeny {?number}
 * @param       scrollbar {?boolean}
 * @param       resize  {?boolean}
 * @param       toolbar {?boolean}
 * @param       menubar {?boolean}
 * @param       location    {?boolean}
 * @param       directories {?boolean}
 * @returns     {Object} New DOMWindow element
 *
 * @description CHANGELOG
 * 1.1
 * - rewrote some parts to make it more lisible and performant
 * - Closure Compiler ADVANCED_OPTIMIZATIONS compliant
 */
function PopUp(strUrl, strWindowName, width, height, screenx, screeny, scrollbar, resize, toolbar, status, menubar, location, directories) {
    var strWindowFeatures = '',
        w = window;

    /*
     * !!VARIABLE converts an element into a boolean
     * It's a shorthand to ease the reading
     */
    strWindowFeatures += 'scrollbar=' + (!!scrollbar ? 'yes' : 'no');
    strWindowFeatures += ',resize=' + (!!resize ? 'yes' : 'no');
    strWindowFeatures += ',toolbar=' + (!!toolbar ? 'yes' : 'no');
    strWindowFeatures += ',status=' + (!!status ? 'yes' : 'no');
    strWindowFeatures += ',menubar=' + (!!menubar ? 'yes' : 'no');
    strWindowFeatures += ',location=' + (!!location ? 'yes' : 'no');
    strWindowFeatures += ',directories=' + (!!directories ? 'yes' : 'no');

    if (parseInt(width, 10) > 0) {
        strWindowFeatures += ',width=' + width;
    }

    if (parseInt(height, 10) > 0) {
        strWindowFeatures += ',height=' + height;
    }

    if (parseInt(screenx, 10) >= 0) {
        strWindowFeatures += ',screenX=' + screenx;
        strWindowFeatures += ',left=' + screenx;
    }

    if (parseInt(screeny, 10) >= 0) {
        strWindowFeatures += ',screenY=' + screeny;
        strWindowFeatures += ',top=' + screeny;
    }

    if (strUrl.indexOf("partner=EMP") > 0) {
        var qualification = '',
            cp = '';

        param = w.location.search.slice(1, window.location.search.length);

        first = param.split("&");
        for (i = 0; i < first.length; i++) {
            second = first[i].split("=");
            if (second[0] == "qualification") {
                qualification = second[1];
            }
            else if (second[0] == "cp") {
                cp = second[1];
            }
        }

        if (qualification) {
            strUrl += "&qualification=" + qualification;
        }
        if (cp) {
            strUrl += "&cp=" + cp;
        }
    }

    return w.open(
        strUrl,
        strWindowName,
        strWindowFeatures);
}

//forces the creation of the function for Closure Compiler with ADVANCED_OPTIMIZATIONS enabled
window['PopUp'] = PopUp;

/*************************************************************************
    * DisplayPopin
    * TODO : description + author
    * @author Unknown
    * 
    * @param		{string}	namePopin
    * @param 	{string}  url
    * @param 	{string} 	nameClass
****************************************************************************/
function DisplayPopin(namePopin, url, nameClass) {
    var sel = $(":first-child:first", document.body);
    sel.before('<div id="' + namePopin + '" class="' + nameClass + '"></div>');
    $('#' + namePopin).load(url, function(html) { $('#' + namePopin).html(html); });        
    return false;
}

/*************************************************************************
    * DisplaySinglePopin
    * Create (if not exists) a container with the content of an url 
    * 
    * @author Boris Schapira
    * 
    * @param	{string}	namePopin	Id of the popin to create
    * @param {string} 	url				URL of the content to load
    * @param {string} 	nameClass 	Class name to apply on the container
****************************************************************************/
function DisplaySinglePopin(namePopin, url, nameClass, selectorTarget) {
    if ($(selectorTarget + ' > #' + namePopin).length == 0) {
        var sel = $(":first-child:first", selectorTarget);
        sel.before('<div id="' + namePopin + '" class="' + nameClass + '"></div>');
    }
    else {
        $('#' + namePopin).empty();
    }

    $.ajax({
        type: "GET",
        url: url,
        success: function (html) {
            $('#' + namePopin).html(html);
            $.event.trigger("ajaxComplete");
        }
    });

    if (typeof $.fn.openPopIn != 'undefined' && $('#' + namePopin).length > 0) {
        $('#' + namePopin).openPopIn();
    }

    return false;
}

/*************************************************************************
* DisplayLoadedPopin
* Display a popin from o loaded Html content (creating a new container if 
* not already exists)
* 
* @param {string}	namePopin	Id of the popin to create
* @param {string} 	htmlContent Loaded Html content
* @param {string} 	nameClass 	Class name to apply on the container
****************************************************************************/
function DisplayLoadedPopin(namePopin, htmlContent, nameClass, selectorTarget) {
    if ($(selectorTarget + ' > #' + namePopin).length == 0) {
        var sel = $(":first-child:first", selectorTarget);
        sel.before('<div id="' + namePopin + '" class="' + nameClass + '"></div>');
    }	
    $('#' + namePopin).html(htmlContent);
    
    if (typeof $.fn.openPopIn != 'undefined' && $('#' + namePopin).length > 0) {
        $('#' + namePopin).openPopIn();
    }	
    return false;
}

/*************************************************************************
    * DisplayWaitingControl
    * TODO : description + author
    * @author Unknown
    * 
    * @param		{string}	namePopin
    * @param 	{string}  url
    * @param 	{string} 	nameClass
    * @param 	{string} 	nameButton
****************************************************************************/
function DisplayWaitingControl(namePopin, url, nameClass, nameButton) {
    var sel = $(":first-child:first", document.body);
    sel.before('<div id="' + namePopin + '" class="' + nameClass + '"></div>');
    
    $('#' + namePopin).load(url, function(html) {
        $('#' + namePopin).html(html);
        $('#' + namePopin).show();
        $('#Envoyer_OnClick').attr('value', '1');
        $('#Envoyer_OnClick').parents('form').submit();

    });

    return false;
}

/*************************************************************************
    * ClosePopin
    * TODO : description + author
    * 
    * @author Unknown
    * 
    * @param		{string}	nameButton
    * @param 	{string}	namePopin
****************************************************************************/
// TODO : Obsolete, use $.fn.closePopIn() from popin.ui.js (R1)
function ClosePopin(nameButton, namePopin) {
        $('#' + nameButton).click(function(){
                $('#'+ namePopin).hide();
                return false;                         
              });
}

/*************************************************************************
* DisplayVideo
* 
* @author Unknown
* 
****************************************************************************/
function DisplayVideo(s) {
    // FlixMedia
    if ($('#DivFlixMedia').find('a').attr('href') != '') {
        eval($('#DivFlixMedia').find('a').attr('href'));
    }
    // Cdiscount videos
    if ($("[id*=productVideo]").attr('rel') != '') {
        eval($("[id*=productVideo]").attr('rel'));
    }
} 





