/* Built by crotante
 * 
 * This file contains functions for interacting with the cookie's and also contains an array
 * of all restricted media for the site.
 *
 * Any media which is to be restricted just needs to be placed in the blockedUrls array
 */
var blockedUrls = new Array();
blockedUrls[0] = "EFS_creative_012711.mp4";
blockedUrls[1] = "Creative_Services.mp4";
blockedUrls[2] = "EFS_merchandising_012711.mp4";
blockedUrls[3] = "Merchandising.mp4";
blockedUrls[4] = "SJ_final.mp4";
blockedUrls[5] = "NE_final.mp4";
blockedUrls[6] = "AB_final.mp4";
blockedUrls[7] = "rw.mp4";
blockedUrls[8] = "EFS_photovideo_012711.mp4";
blockedUrls[9] = "Online_Marketing_Data_Analysis.mp4";
blockedUrls[10] = "EFS_fulfillment_012711.mp4";
blockedUrls[11] = "EFS_customercare_012711.mp4";
blockedUrls[12] = "Reporting_Business_Intelligence.mp4";
blockedUrls[13] = "Quicker_to_Launch.mp4";
blockedUrls[14] = "Quicker_to_Ship.mp4";
blockedUrls[15] = "Quicker_to_Profit.mp4";
blockedUrls[16] = "bp.mp4";
blockedUrls[17] = "de.mp4";
blockedUrls[18] = "PJD_final.mp4";
blockedUrls[19] = "EFS_history_012711.mp4";


function getCookie(c_name){
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}
function setCookie(c_name,value,expiredays){
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toUTCString()) +"; secure";
}
/*Given an html element, replace the url with either the optin form, or if on a
 *mobile device, a direct link to the video.
 *
 * */
function swapURL(element){
    var mobile = isMobile();
    var link = element.href;
    var start = link.indexOf("mediaUrl=") + 9;
    if(mobile == true)
        start = link.indexOf("/website/") + 9;

    var end = link.indexOf(".mp4");
    var movieTitle = link.substring(start,end)+".mp4";
    if(isInBlockedUrls(movieTitle) && element.className != "thickbox noblock" ){
        var urlStart = link.indexOf('.php?');
        urlStart += 5;
        link = link.replace("height", "m_height");
        link = link.replace("width", "m_width");
        link = link.substring(urlStart,link.length);
        element.href="/wp-content/themes/twentyten/capture.php?height=350&width=750&"+link;
    }
    else if(isInBlockedUrls(movieTitle) && element.className == "thickbox noblock" && mobile == true)
    {
        start = link.indexOf("mediaUrl=") + 9;
        end = link.indexOf(".mp4");
        movieTitle = link.substring(start,end);
        element.href= "http://efashion.hs.llnwd.net/o33/efs/VIDEO/website/"+movieTitle+".mp4";
    }
}

/**Runs through all anchor tags and replaces any that contain a blocked url from the
 *blockedUrls array
 *
 *Also contains the page to forward to when found restricted content
 * */
function swapUrls() {
    //swap all anchor tags
    var allAnchors = document.getElementsByTagName("a");
    var len = allAnchors.length;
    
    for (var i=0; i<len; i++) {
        swapURL(allAnchors[i]);
    }
    //swap all area tags
    var allAreas = document.getElementsByTagName("area");
    len = allAreas.length;

    for (i=0; i<len; i++) {
        swapURL(allAreas[i]);
    }

}
/**Runs through all anchor tags and replaces specific links with links that are to be forwarded
 * to another page.
 *      forwardee - the link that is TO be replaced
 *      forwarder - the link that is to replace forwardee
 * CURRENTLY NOT IN USE (using forward_template.php)
 * */
function forwardLinks() {
    var allAnchors = document.getElementsByTagName("a");
    var len = document.getElementsByTagName("a").length;
    var link = null;
    var forwardee = "/services/turnkey-ecommerce/" ;
    var forwarder = "/platform/";
    for (var i=0; i < len; i++) {
        link = allAnchors[i].href;
        if(link.indexOf(forwardee) >= 0)
        {
            var newLink = link.replace(forwardee,forwarder)
            allAnchors[i].href = newLink;
        }
    }
}

/** Checks to see if any of the restricted media are in the href of the current anchor tag
 *
 * */
function isInBlockedUrls(url) {
    var bool = false;
    for (var i in blockedUrls)
    {
        if(url.indexOf(blockedUrls[i]) != -1)
        {
            bool = true;
        }
    }
    return bool;
}
/* Checks to see if the device is a mobile device
 * Returns
 * */
function isMobile() {
    var isiPad;
    var isiPhone;
    var isiPod;
    var isAndroid;
    var isBlackBerry;
    var isIEMobile;

    isiPad = navigator.userAgent.match(/iPad/i) != null;
    isiPhone = navigator.userAgent.match(/iPhone/i) != null;
    isiPod = navigator.userAgent.match(/iPod/i) != null;
    isAndroid = navigator.userAgent.match(/android/i) != null;
    isBlackBerry = navigator.userAgent.match(/BlackBerry/i) != null;
    isIEMobile = navigator.userAgent.match(/IEMobile/i) != null;

    if (isiPad == true || isiPhone == true || isiPod == true || isAndroid == true || isBlackBerry == true | isIEMobile == true) {
        return true;
    }else{
        return false;
    }
}
/*  This is used for mobile browsers, replaces all urls containing mp4 with direct links to the videos
 *  returns true if mobile
 */
function swapMobileUrls() {
    //remove all anchor tags with "thickbox" class
    jQuery('.thickbox').removeClass("thickbox");
    //reset link urls to location on IDN
    var allAnchors = document.getElementsByTagName("a");
    var len = document.getElementsByTagName("a").length;
    for (var i=0; i<len; i++) {
        mobileSwap(allAnchors[i]);
    }

    var allAreas = document.getElementsByTagName("area");
    len = document.getElementsByTagName("area").length;
    for (i=0; i<len; i++) {
        mobileSwap(allAreas[i]);
    }
}
/* Changes the href value of an element for mobile
 *
 * */
function mobileSwap(element) {
    var link = null;
    link = element.href;
    if(link.indexOf('.mp4') != -1)
    {
        var start = link.indexOf("mediaUrl=") + 9;
        var end = link.indexOf(".mp4");
        var movieTitle = link.substring(start,end);
        element.href= "http://efashion.hs.llnwd.net/o33/efs/VIDEO/website/"+movieTitle+".mp4";
    }
}
/* This function takes all the links in the client lightboxs, and forwards the links to direct links
 *
 * */
function swapVidUrlsClients() {
    jQuery('.video').unbind();
    //reset link urls to location on IDN
    var allAnchors = document.getElementsByTagName("a");
    var len = document.getElementsByTagName("a").length;
    var link = null;
    for (var i=0; i<len; i++) {
        link = allAnchors[i].name;
        if(link.indexOf('.mp4') != -1)
        {
            var start = link.indexOf("/website/") + 9;
            var end = link.indexOf(".mp4");
            var movieTitle = link.substring(start,end);
            allAnchors[i].href= "http://efashion.hs.llnwd.net/o33/efs/VIDEO/website/"+movieTitle+".mp4";
        }
    }
}
/* Removes lightbox on all anchor and area tags on a page, if they are protected.
 *
 * */
function removeLightBox()
{
    var allAnchors = document.getElementsByTagName("a");
    var len = document.getElementsByTagName("a").length;
    var link = null;
    for (var i=0; i<len; i++) {
        link = allAnchors[i].href;
        if(isMobile() == true && link.indexOf('capture.php?') == -1 && allAnchors[i].getAttribute("class") == "thickbox" )
            allAnchors[i].setAttribute("class", "");

    }

    var allAreas = document.getElementsByTagName("area");
    len = document.getElementsByTagName("area").length;
    for (i=0; i<len; i++) {
        link = allAreas[i].href;
        if(isMobile() == true && link.indexOf('capture.php?') == -1 && allAreas[i].getAttribute("class") == "thickbox" )
            allAreas[i].setAttribute("class", "");
    }
}
/* Handles forwarding to a HTTPS version of the current page.
 * Includes opening the intended lightbox in the new https page
 * */
function forwardHttps (){
    //forward to a secure page when a thickbox is clicked, which has href containing capture.php, and NOT class of noblock
        jQuery('.thickbox').unbind();
        jQuery('.thickbox[href*="capture.php"]').not('.noblock').bind('click',function(){
            var query = jQuery(this).attr('href');
            query = query.substring(query.indexOf("?")+1,query.length);
            window.location="https://"+window.location.host+window.location.pathname+"?form=popup&"+query;
        });
}
/* Checks to see if the restricted media cookie exists yet, if it doesn't replace
 * all appropriate urls with the link to the optin
 * */
function checkForCookie(){
    optin=getCookie("optin");
    if(optin.indexOf("approved")== -1 )
    {
        swapUrls();
        forwardHttps();
        if(isMobile() == true)
        {
            //remove thickbox on ipad for all (except videos because user has not opted in yet)
             jQuery(':not(.thickbox[href*="capture.php"])').removeClass('thickbox');
        }
    }
        
    //this is for mobile browsers only
    if(optin.indexOf("approved")!= -1 && isMobile() == true)
        swapMobileUrls();     
}



