//<reference path="/static/js/jquery/jquery-1.3.2.js" />

if (window.jQuery == null) {    
        addJQuery();
} else{
    jQuery(document).ready(renderDeeplinks);
    }

function addJQuery() {
    var e = document.createElement("script");
    var baseUrl = getBaseURL();
    var protocol = baseUrl.split(':')[0];
    e.src = protocol+"://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js";
    e.type = "text/javascript";
    
    if (document.getElementsByTagName("head").length > 0)
        document.getElementsByTagName("head")[0].appendChild(e);
    else if (document.getElementsByTagName("body").length > 0) {
        var bodyTag = document.getElementsByTagName("body")[0];
        if (bodyTag.hasChildNodes())
            bodyTag.insertBefore(e, bodyTag.firstChild);
        else
            bodyTag.appendChild(e);
    }
    jQueryOnLoadEventChecker = window.setInterval(onJQueryLoad, 50);
}

var jQueryOnLoadEventChecker;
function onJQueryLoad() {
    if (window.jQuery != null) {
        jQuery.noConflict();
        window.clearInterval(jQueryOnLoadEventChecker);
        jQuery(document).ready(renderDeeplinks);
    }   
}

function renderDeeplinks(){
    var linksList = new Array();
    jQuery("*[class *= '__promoPrices_']").html('<img src="http://promos.opodo.co.uk/promos/static/images/ajax-loader_small.gif" style="border: 0px;" />');
    getLinksIds(linksList,jQuery("*[class *= '__promoPrices_']").get());
    if(linksList.length>0)
        processDeeplinks(linksList);
}


function getBaseURL() {
    var scriptTags = document.getElementsByTagName("script");
    var scriptSrc = "";
    for (var i = 0; i < scriptTags.length; i++) {
        scriptSrc = scriptTags[i].getAttribute("src");
        if (scriptSrc != null && scriptSrc!="" && scriptSrc.indexOf("promoPrices.js")>0)
            break;
    }
    if (scriptSrc == null || scriptSrc == "")
        return ""; 
    return scriptSrc.split('/')[0] + '//' + scriptSrc.split('/')[2];
}

function getLinksIds(linksList,placeholders){
    for (var i = 0; i < placeholders.length; i++) {
        var elementCss = jQuery(placeholders[i]).attr("class").split(" ");
        for(var j=0;j<elementCss.length;j++)
            linksList.push(elementCss[j].substring('__promoPrices_'.length));
    }
}

function processDeeplinks(linksList){
    var uniqueLinks = new Array();
    for(var i=0;i<linksList.length;i++)
        if(jQuery.inArray(linksList[i],uniqueLinks)==-1)
            uniqueLinks.push(linksList[i]);

    var dlUrl = getBaseURL()+"/promos/promoPrices?ids="+uniqueLinks.join(",");
    var e = document.createElement("script");
    e.src = dlUrl;
    e.type="text/javascript";
    document.getElementsByTagName("body")[0].appendChild(e); 
}


function onDeeplinksDownloaded(deeplinks){
    for(var i=0; i< deeplinks.length;i++){
        jQuery(".__promoPrices_" + deeplinks[i].id).each(
            function() {
                if (this.nodeName.toUpperCase() == "A") {
                    if (deeplinks[i].price > 0)
                        jQuery(this).attr("href", deeplinks[i].deeplink);
                    else
                        jQuery(this).removeAttr("href");
                } else if(deeplinks[i].price > 0) {
                    jQuery(this).css("cursor", "pointer");
                    jQuery(this).bind("click", { deeplinkURL: deeplinks[i].deeplink, openNewWindow: jQuery(this).is('.openNewWindow') }, function(eventObject) {
                        if (eventObject.data.openNewWindow)
                            window.open(eventObject.data.deeplinkURL);
                        else
                            window.location = eventObject.data.deeplinkURL;
                        return false;
                    });
                }
            }
                );
                
        jQuery(".__promoPrices_"+deeplinks[i].id).each(
            function(){
                jQuery(this).html(deeplinks[i].price>0 ? "&pound;"+deeplinks[i].price:"n/a");}
                );
    }
    
}

