﻿/*-------------------------------------------------------
############ ASOS JS LIBRARY version 1.3 ################
##### created by: Pavel Jacko  ##########################
#### released: 04/06/2008 ###############################
#### updated: 10/06/2008 ################################
--------------------------------------------------------*/
// #### <DEBUGGING> #########################################################
function trace(_msg){
    if(_C.testing==true)alert(_msg);
}
// #### </DEBUGGING> #########################################################

// #### <CREATIVE> #########################################################
_C={
    jsData:[],
    //TEST: http://test.asos.com/images/ 
    //LIVE: http://images.asos.com/ 
    prefix:"http://images.asos.com/",
    hashVal:function(){return window.location.hash.substr(1);},
    testing:false,
    asos:true,
    disableResize:false,
    
    //###Improt function
    importModule:function(_namespace){
        var fileref=document.createElement('script');
        fileref.setAttribute("type","text/javascript");
        fileref.setAttribute("src", _C.CORE.returnNS(_namespace));
        if (typeof fileref!="undefined"){
            document.getElementsByTagName("head")[0].appendChild(fileref);
        }
        trace('created import');
    }
};

_C.CORE={

// import vars
returnNS:function(_string){
    var prefix="scripts";
    switch(_string){
        case "creative.flash":
        return prefix+"/CREATIVE_FLASH_Lib.js";
    }
}
,
//###object detection: $(object,[id|class|tag],[index]) ---------
$:function(object,accessby,val){	    
  //1. if the function is called in the context of an object
  //make it the root for DOM search	    		
	if(this==_C.CORE || this==ASOS) {var root=document;}
	else {var root=this;}		    
	var obj;     
  switch(accessby){
    //2. if class was declared 
    //search with regular expressions for all elements of class  
    case "class":     
      var els=root.getElementsByTagName("*");
      var pattern = new RegExp("(^|\\s)"+object+"(\\s|$)");
      var classElements = new Array();
      var elsLen = els.length;
      for (i = 0, j = 0; i < elsLen; i++) {
	      if ( pattern.test(els[i].className) ) {
            classElements[j] = els[i];
            j++;
        }
      }       
      obj=classElements;
    break;        
    //2. if tag was declared 
    //search for all tags
    case "tag":
      obj=root.getElementsByTagName(object);
    break;      
    //3. If this was neither class nor tag, treat it as object
    //after finding, augument object with a reference to $ function
    //and rollover as well         
    default:
      if(!_C.CORE.isArray(object)) {    
        if(obj=root.getElementById(object)) {
          obj.$=this.$;
        }
      }
      else 
      {
        var obj=[];
        for (var i in object) {
          obj.push(this.$(object[i]));
        }	
      }
      //4. if index was specified, do not return whole array
      //but just one element
  }	      
	if (val!=undefined) {obj=obj[val];}
  if (obj) {
    obj.typ=(accessby?accessby:"id");
	}
  return obj;
},
//###Array Check    
isArray:function(what) {
  return !!(what && (what.constructor || what.item) && (what.constructor.toString().indexOf("Array")>=0 || what.item));
},
//###uploading images (_recording:Boolean sets if image will be passed to flash or not
uploadImg:function(_id,_src){
  if(_C.FLASH.enabled)_C.jsData.push([_id,_src]);
  else this.$(_id).src=_src;
}
,
addEvent:function (obj, type, fn) {	   
    if (obj.attachEvent ) {
        obj["e"+type+fn] = fn;
        obj[type+fn] = function(){obj["e"+type+fn](window.event);};
        obj.attachEvent("on"+type, obj[type+fn]);
    }
    else {
        obj.addEventListener( type, fn, false );
    }
}
, 
removeEvent:function(obj, type, fn) {
     if (obj.detachEvent ) {
      obj.detachEvent("on"+type, obj[type+fn]);
       obj[type+fn] = null;
    } else {
       obj.removeEventListener( type, fn, false );
       }
}
,   
quickhelp:function(obj){
    var output="";
    for (var i in obj) {output+=i+" ["+typeof obj[i]+"]"+"\n";}
    alert(output);
}
,
//XML converter
convertToXml:function(_parentId,_parentXml,_element,_sendId,_sendClass,_sendHref){
    var $xmlHref="";
    var $xmlClass="";
    var $xmlId="";
    var $xmlNode="";
    var $navArr="";
    
    var $elements=_C.CORE.$(_parentId).getElementsByTagName(_element);
    for(i=0;i<$elements.length;i++){
        if(_sendId){
            $xmlId=$elements[i].getAttribute('href');
            $xmlId=" id=\""+$xmlId+"\"";
        }
        if(_sendHref){
            $xmlHref=$elements[i].getAttribute('href');
            $xmlHref=" href=\""+$xmlHref+"\"";
        }
        if(_sendClass){
            $xmlClass=$elements[i].getAttribute('href');
            $xmlClass=" class=\""+$xmlClass+"\"";
        }   
         
        $xmlNode=$elements[i].innerHTML;
        //var $link="<a href=\""+$xmlAttribute+"\" class=\""+$xmlClass+"\" >"+$xmlNode+"</a>";
        var $link="<"+_element + $xmlId + $xmlHref + $xmlClass+">"+$xmlNode+"</"+_element+">";
        trace($link);
        $navArr+=$link;
    }
    $navArr="<"+_parentXml+">"+$navArr+"</"+_parentXml+">";
    trace($navArr);
    return $navArr;
}
,
// init() function
initialize:function(){
    
    var $version=swfobject.hasFlashPlayerVersion(_C.FLASH.version);
    if($version){
        if(_C.asos){
            _C.CORE.$("creativeShadow").style.height = _C.FLASH.height + "px";
        }
        _C.FLASH.enabled = true;
        _C.TRIGGERS.imageUpload();
        _C.FLASH.attributes.name = _C.FLASH.attributes.id;
        _C.FLASH.show();
        _C.TRIGGERS.flash(); 
    } 
    else{
        if(_C.FLASH.dynamicLoad){
            
            _C.TRIGGERS.imageUpload();
        }
        else{
            _C.FLASH.showAlt();
            
        }
        _C.TRIGGERS.html();
    }
}
,
//converting flash object
thisMovie:function(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
},
//align height
alignHeight:function(_alignTarget,_alignSource,_checkHeight,_addHeight) {
    var $nav = _C.CORE.$(_alignSource).offsetHeight;
   var $pg = _C.CORE.$(_alignTarget).offsetHeight;
   
    if($pg < ($nav + _checkHeight)){
        var $h = ($nav + _checkHeight + _addHeight)+"px";
        _C.CORE.$(_alignTarget).style.height = $h;
        if(arguments[4] != undefined || arguments[4] != null){
            _C.CORE.$(arguments[4]).style.height = $h;
        }
    }
    else{
        var $h2 = _C.CORE.$(_alignTarget).style.height;
        _C.CORE.$(_alignSource).style.height = $h2;
       _C.CORE.$(arguments[4]).style.height = $h2;
       _C.CORE.$("ContentNav").style.height = $h2;
    }
},
alignHeightASOS:function(){
    _C.CORE.alignHeight("creativeShadow","ContentNavContainer",30,2,"creativeContent");
},
resizeFix:function(){
    
    //commented out due to site redesign Jan 09
    /*var $con = _C.CORE.$("siteContainer");
    var $html  = _C.CORE.$("html","tag")[0];
    var $body = _C.CORE.$("body","tag")[0];
    $con.style.width = (1112 + _C.CORE.$("siteWrapper").offsetLeft) + "px";*/
}
,
resizeHeightFix:function(){
    
    /*
    var $html = _C.CORE.$("siteContainer");
    if($html.style.overflowX==null){
        $html.style.overflow = "hidden";
        trace(_C.CORE.$("ContentCentre").offsetHeight);
        $html.style.height = (_C.CORE.$("ContentCentre").offsetHeight + 128) + "px";
    }
    else{
        $html.style.height = "auto";
        $html.style.overflow = "hidden";
        $html.style.overflowX = "hidden";
    }*/
    

},
resizeBlog:function()
{
    if(_C.CORE.blogCreated)
    {
        if (typeof window.innerHeight != 'undefined')
        {
            $height = window.innerHeight;
        }
        else
        {
             var $body = _C.CORE.$("html","tag")[0];
             $height = $body.offsetHeight;
        }
        if($height>_C.CORE.$("ContentCentre").offsetHeight)
        {
            _C.CORE.$("blogBanner").style.top = _C.CORE.$("ContentCentre").offsetHeight - 158 + "px";
        }
        else
        {
            if($height>473)
            {
                _C.CORE.$("blogBanner").style.top = $height-168 + "px";
            }
            else{
                _C.CORE.$("blogBanner").style.top = "300px";
            }
        }
    }
}
,
initStart:function(){
    trace('initialize');
    _C.TRIGGERS.init();
    if(!_C.disableResize){
        var version = navigator.userAgent;
        var findIE6 = version.indexOf('MSIE');
        if(findIE6 == -1)
        {
            if(_C.CORE.$("content_nav_shadow","class")[0]!=null){
                var $n = _C.CORE.$("content_nav_shadow","class")[0];
                var $i=  "url(http://images.asos.com/homepage/bg.png)";
                $n.style.opacity = 1;
                $n.style.backgroundImage=$i;
                $n.style.backgroundColor = "transparent";
            }
        }
        _C.CORE.resizeHeightFix();
        _C.CORE.resizeFix();
        //_C.CORE.resizeBlog();
        window.onresize = _C.CORE.resizeFix;
    }
}
,
hideNav:function(){
    //swfobject.createCSS(".content_homepage_nav", "display:none;");
    _C.CORE.$("ContentNav").style.display = "none";
}
,
createBlog:function(_link,_blogImg,_lLink,_rLink){
        var $blog = document.createElement("div");
        $blog.id="blogBannerContent";
        $blog.innerHTML = "";
        $blog.style.position = "absolute";
        $blog.style.backgroundColor="#000000";
        $blog.style.top = "0px";
        $blog.style.left = "0px";
        $blog.style.height = "30px";
        $blog.style.width = "1260px";
        
        var blogLabel = document.createElement("a");
        blogLabel.href = _link;
        blogLabel.innerHTML = "<img src=\""+_C.prefix+_rLink+"\" alt=\"read all posts\" />";
        blogLabel.style.position = "absolute";
        blogLabel.style.left = "333px";
        $blog.appendChild(blogLabel);
        
        var blogLink = document.createElement("a");
        blogLink.href = _link;
        blogLink.innerHTML = "<img src=\""+_C.prefix+_lLink+"\" alt=\"read all posts\" />";
        blogLink.style.position = "absolute";
        blogLink.style.left = "1005px";
        $blog.appendChild(blogLink);
        
        var arrow = document.createElement("span");
        arrow.innerHTML = "&gt;";
        arrow.style.position = "absolute";
        arrow.style.top = "7px";
        arrow.style.left = "455px";
        arrow.style.height = "30px";
        arrow.style.width = "10px";
        arrow.style.color = "#FFFFFF";
        $blog.appendChild(arrow);
        
        var arrow2 = document.createElement("span");
        arrow2.innerHTML = "...&gt;";
        arrow2.style.position = "absolute";
        arrow2.style.top = "7px";
        arrow2.style.left = "980px";
        arrow2.style.height = "30px";
        arrow2.style.width = "10px";
        arrow2.style.color = "#FFFFFF";
        $blog.appendChild(arrow2);
        
        var blogMask = document.createElement("div");
        blogMask.id="blogMask";
        blogMask.innerHTML = "";
        blogMask.style.position = "absolute";
        blogMask.style.top = "0px";
        blogMask.style.left = "470px";
        blogMask.style.height = "30px";
        blogMask.style.width = "507px";
        blogMask.style.overflow = "hidden";
        $blog.appendChild(blogMask);
        
        var blogStrip = document.createElement("div");
        blogStrip.id = "blogStrip";
        blogStrip.style.position = "absolute";
        blogStrip.style.height = "30px";
        blogStrip.style.marginTop = "7px";
        blogStrip.style.left = "0px";
        blogStrip.style.top = "0px";
        blogMask.appendChild(blogStrip);
        
        var blogCon = document.createElement("div");
        blogCon.id="blogCon";
        blogCon.innerHTML = "";
        blogCon.style.position = "absolute";
        blogCon.style.top = "0px";
        blogCon.style.height = "40px";
        blogStrip.appendChild(blogCon);

        
        var blogCon2 = document.createElement("div");
        blogCon2.id="blogCon2";
        blogCon2.innerHTML = "";
        blogCon2.style.position = "absolute";
        blogCon2.style.top = "0px";
        blogCon2.style.height = "40px";
        blogStrip.appendChild(blogCon2);

        /*var blogImg = document.createElement("img");
        blogImg.id="blogImg";
        blogImg.src = _C.prefix+_blogImg;
        blogImg.style.position = "absolute";
        blogImg.style.top = "-28px";
        blogImg.style.left = "949px";
        //blogImg.style.width = "40px";
        //blogImg.style.height = "40px";
        $blog.appendChild(blogImg);*/
        
        _C.CORE.$("blogBanner").innerHTML = "";
        _C.CORE.$("blogBanner").appendChild($blog);
        _C.CORE.blogCreated = true;
},
blogEntryWidth:0,
blogCreated:false,
addBlogEntry:function(_id,_message,_link){
    var $entry = _C.CORE.$("blogCon").innerHTML;
    var $link = "<a id=\"BlogLink"+_id+"\" class=\"blog_link\" href=\""+_link+"\">"+_message+" &raquo;</a>";
    
    $entry+=$link;
    _C.CORE.$("blogCon").innerHTML = $entry;
    _C.CORE.$("blogCon2").innerHTML = $entry;
    _C.CORE.$("blogCon2").style.left = _C.CORE.$("blogCon").offsetWidth + "px";
    
    _C.CORE.$("blogStrip").style.width = (_C.CORE.$("blogCon").offsetWidth*10)+"px";
   _C.CORE.blogEntryWidth = _C.CORE.$("blogCon").offsetWidth;
    //alert(_C.CORE.$("blogCon").offsetWidth);
},
setHash:function(_value){
    window.location.hash = _value;
}
};

_C.TRIGGERS = 
{
    imageUpload:function(){trace('#WARNING: missing imageUploadTrigger');},
    html:function(){trace('#WARNING: missing htmlTrigger');},
    flash:function(){trace('#WARNING: missing flashTrigger');},
    init:function(){trace('#WARNING: missing initTrigger');},
    DOMLoad:function(){trace('#WARNING: missing domLoadTrigger');}
};

_C.FLASH = 
{
    prefix:""
    ,
    dynamicLoad:true
    ,
    // flash checker
    enabled:false
    ,
    url:""
    ,
    swf:""
    ,
    width:"1260"
    ,
    height:"500"
    ,
    version:"99.0.0"
    ,
    flashVars:
    {
    }
    ,
    params:
    {
       menu:"false",
       allowscriptaccess:"always",
       wmode:"opaque" 
    }
    ,
    attributes:
    {
        id:"creative_flash",
        name:"creative_flash"
    }
    ,
    hideNav:false
    ,
    fix:function(){window[_C.FLASH.attributes.id] = new Object();}
    ,
    //show flash
    show:function(){
        _C.FLASH.fix();
        trace(_C.hashVal+":hash value");
        var $hash = _C.hashVal();
        if($hash=="" || $hash==undefined)$hash="none";
        trace(_C.FLASH.attributes.id);
        if(_C.FLASH.dynamicLoad){
            var $menuDataXml = _C.CORE.convertToXml("ContentNavContainer","DATA","a",false,true,true);
            $menuDataXml= escape($menuDataXml);
            //alert(_C.flashData.params.menu);
            trace($menuDataXml);
            if(_C.FLASH.hideNav) _C.CORE.hideNav();
        }
        
        //alert(_C.FLASH.flashVars.hashVal);
        var $url;
        if(_C.asos){
            $url = _C.FLASH.url;
        }
        else{
           $url = ""; 
        }
        _C.FLASH.flashVars = {
            prefix:_C.FLASH.url,
            hashVal:$hash,
            jsData:_C.jsData,
            menuData:$menuDataXml,
            mainPrefix:$url
        }
        swfobject.embedSWF($url + _C.FLASH.swf, _C.FLASH.attributes.id,_C.FLASH.width, _C.FLASH.height, _C.FLASH.version, "http://www.asos.com/images/htmlpages/flash_install_data/expressinstall_w.swf", _C.FLASH.flashVars, _C.FLASH.params , _C.FLASH.attributes);
    }
    ,
    showAlt:function(){
        var $f = _C.CORE.$(_C.FLASH.attributes.id);
        $f.innerHTML = "<div id=\"creativeFlashAlt\"><div id=\"creativeFlashAltText\"><strong>You need Flash player "+_C.FLASH.version+" or later to view this content.</strong><br/>You can download latest version of flash player here:<br /><a href=\"http://www.adobe.com/go/getflashplayer\"><img style=\"border:none;margin-top:5px;\" src=\"http://images.asos.com/htmlpages/flash_install_data/getFlash.gif\" alt=\"Get Flash Player here\" /></a></div></div>";
        $f.style.width = _C.FLASH.width + "px";
        $f.style.height = _C.FLASH.height + "px";
    }
};
//########################
//swfobject.addDomLoadEvent(_C.CORE.initStart);