/**
Core Object
**/
C = {
    //
    listeners:[],
    //
    listenerLocks:[],
    //array of listeners
    triggers:[],
    //namespace resources
    resources:[],
    //locks for asynchronous loading
    resourceLocks:[],
    //all namespaces (DOM inclide 1)
    finalResourcesCount:0,
    //currently loaded namespaces
    currentResourcesCount:0,
    //
    LOADED:"loaded",
    //
    JS_PREFIX:"/assets/asosCom/js/libs/",
    //
    addResource:function(ns){
        //alert("addResource:"+ns);   
        var canAdd = !C.isImported(ns);
        if(canAdd==true)
        {
            C.finalResourcesCount++;
            C.resources.push(C.JS_PREFIX+ns+".js");
            C.resourceLocks.push(false);//unlocked for now
        }
        
    },
    //
    loadResources:function(){
        $(document).ready(function() {
            //alert("loadResources, total:"+C.resources.length);            
            for(var i=0;i<C.resources.length;i++)
            {
                
                if(C.resourceLocks[i]==false)//check if this was not push in other async operation
                {
                    $.getScript(C.resources[i],C.checkResources);
                    C.resourceLocks[i]=true;//if not, lock it
                    //alert("loading:"+C.resources[i]);
                }
            }
        
        }); 
    },
    //
    addEventListener:function(type,trigger)
    {
        var lCount=C.listeners.length;
        var isListening=false;
        
        //check if listener is registered 
        for(var i=0;i<lCount;i++)
        {
            if(C.listeners[i][0]==type)
            {
                //console.log("LISTEN1");
                //C.listeners[i][]
                C.listeners[i][1].push([trigger,false]);
                //console.log(C.listeners[i][1]);
                var isListening=true;
                break;
            }
                
        }
        //if not, register it
        if(isListening==false)
        {
            //console.log("LISTEN"+type);
            C.listeners.push([type,[[trigger,false]]]);
            //console.log("LISTEN::"+C.listeners[0]);
            //checkTriggers();
        }
    },
    
    //check all resources after namespace has been loaded
    checkResources:function(){
        
        //console.log("checking resources:",C.currentResourcesCount,C.finalResourcesCount);
        C.currentResourcesCount++
        //alert("loaded:"+C.currentResourcesCount+" from: "+C.finalResourcesCount);
        if(C.finalResourcesCount==C.currentResourcesCount)
        {
            //alert("asos.js.corelib:READY");
            C.dispatchEvent(C.LOADED);
        }
        
    },
    //
    dispatchEvent:function(name)
    {
        for(var i=0;i<C.listeners.length;i++)
        {
            //console.log("triggerss"+C.listeners[0][1]);
            if(C.listeners[i][0]==name)
            {
                for(var i2=0;i2<C.listeners[i][1].length;i2++)
                {
                    if(C.listeners[i][1][i2][1]==false)
                    {
                        C.listeners[i][1][i2][0].apply(this,[C]);
                        C.listeners[i][1][i2][1]=true;//lock for listener so it won't fired more than once
                    }
                    
                    //console.log(C.listeners+"trigger"+C.listeners[i][1][i2]);
                }
            }
        }
    }
    ,
    isImported:function(ns)
    {
        for(var i=0;i<C.resources.length;i++)
        {//check if namespace was not added twice accidently by developer
            if(C.resources[i]==ns)
            {
                return true;
            }
        }
        return false;
    }
    ,
    isLoaded:function(ns)
    {
        
    }
}

/**
FACADE FOR LEGACY content
**/
_C={
    CORE:
    {
        initialize:function(){
            if(_C.FLASH.dynamicLoad==false)
            {
                _C.TRIGGERS.setUpLegacyFlash();
            }
        },
        alignHeightASOS:function(){}
    },
    TRIGGERS:
    {
        init:{},
        setUpLegacyFlash:function(){
            
            if (swfobject.hasFlashPlayerVersion(_C.FLASH.version)) {
                $('#creativeContent').html("<div id=\"flashCon\"></div>");
                $('#creativeContent').css({"overflow":"hidden","height":_C.FLASH.height+"px"});
                $('#flashCon').css({"width":_C.FLASH.width+"px","height":_C.FLASH.height+"px"});

                if(_C.FLASH.width!=788)
                {
                    $('#flashCon').css("margin-left",974-_C.FLASH.width+"px");
                    $('#creativeContent').css("width","974px");
                }
                else
                {
                    $('#creativeContent').css("width","788px");
                }
                $('#flashCon').html("<div id='creative_flash'></div>");
                swfobject.embedSWF(_C.FLASH.url+_C.FLASH.swf, 'creative_flash',_C.FLASH.width+"px", _C.FLASH.height+"px", _C.FLASH.version, "http://www.asos.com/images/htmlpages/flash_install_data/expressinstall_w.swf",{},{wmode:"transparent"},{});
                //alert("asasasas");
            }
            else
            {
                if(_C.FLASH.width>974)
                {
                    _C.FLASH.width = 974;
                }
                $('#creative_flash').html(_C.FLASH.altContent());
                //alert("bum");
            }
        }
        
    },
    FLASH:
    {
        dynamicLoad:true,
        swf:"",
        url:"",
        attributes:{id:""},
        width:"1260",
        height:"600",
        version:"9.0.0",
        altContent:function()
        {return "<div style='width:974px;height:"+_C.FLASH.height+"px;'></div><div id=\"creativeFlashAlt\" style=\"position:absolute;left:0px;top:0px;width:"+_C.FLASH.width+"px;background-color:#EEEEEE;height:"+_C.FLASH.height+"px;\"><div id=\"creativeFlashAltText\" style=\"text-align:center;width:400px;margin-left:"+(_C.FLASH.width/2-200)+"px;margin-top:"+(_C.FLASH.height/2-50)+"px;\"><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>";}
    }
}


