var objecT;

function ewSync(input){
    objecT = input;
    var adserved = 0;
    var priority = 99;
    var i;
    for (i = 0; input[i]; i++){
        if ((input[i].Companion.height === "60" && input[i].Companion.width === "468") && (priority > 1)){
            adserved =i;
            priority = 1;
            //alert("now using "+i+", at priority "+priority);
        }
        else if ((input[i].Companion.height === "45" && input[i].Companion.width === "620") && (priority > 2)){
            adserved = i;
            priority = 2;
            //alert("now using "+i+", at priority "+priority);
        }
        else if ((input[i].Companion.height === "50" && input[i].Companion.width === "620") && (priority > 3)){
            adserved = i;
            priority = 3;
            //alert("now using "+i+", at priority "+priority);
        }
    }
    i = adserved;
    if (priority !== 99){
        var IframeResource;
        var HTMLResource;
        var StaticResourceType;
        var StaticResourceText;
        var ClickThrough;
        var TrackingEvent;
        try {
            IframeResource = input[i].Companion.IFrameResource.text;
            HTMLResource =  input[i].Companion.HTMLResource.text;
            StaticResourceType = input[i].Companion.StaticResource.creativeType;
            StaticResourceText = input[i].Companion.StaticResource.text;
            ClickThrough = input[i].Companion.CompanionClickThrough.text;
            TrackingEvent = input[i].Companion.TrackingEvents.Tracking.text;
        } catch(e){}

        if (IframeResource) {
            document.getElementById("companionAd").innerHTML = "<iframe src='"+IframeResource+"' marginheight='0px' marginwidth='0px' scrolling='no' frameborder='0' style='border:none;overflow:hidden;width:"+input[i].Companion.width+"px;height:"+input[i].Companion.height+"px;'></iframe>";
        }
        else if (HTMLResource) {
            document.getElementById("companionAd").innerHTML = HTMLResource;
        }
        else if (StaticResourceType.indexOf("shockwave-flash") !== -1) {
            document.getElementById("companionAd").innerHTML = "<embed src='"+StaticResourceText+"' width='"+input[i].Companion.width+"' height='"+input[i].Companion.height+"'/>";
        }
        else if (StaticResourceType.indexOf("image") !== -1) {
            if (ClickThrough) {
                document.getElementById("companionAd").innerHTML = "<a href='"+ClickThrough+"' target='_blank'><img src='"+StaticResourceText+"'/></a>";
            }else {
                document.getElementById("companionAd").innerHTML = "<img src='"+StaticResourceText+"'/>";
            }
        }
        if (TrackingEvent) {
            document.getElementById("companionAd").innerHTML += "<iframe src='"+TrackingEvent+"' style='display:none;'></iframe>";
        }
        document.getElementById("companionAd").style.display = "block";
        //do something to input[i]['TrackingEvents']['Tracking']['event'] creativeview goes here
    }
}

