// reset the ad for CPC ads
function doSetTimeout(){
     window.adtimeout = setTimeout(doLowProfile, 15000);
}

// replace the Banner Ad with CPC and set the timer
function doLowProfile(){
     $("#bannerAdFull").hide();
     $("#bannerAdLow").load(cpcURL);
     $("#bannerAdLow").show();
     setTimeout(doLowProfile, 20000);
}

function showTapNav(){
    $("#more").text("Close Menu");
    $("#tapNav").show();
}

function hideTapNav(){
    $("#more").text("Features");
    $("#tapNav").hide();
}


function updateOrientation()
{
    var displayStr = "Orientation : ";

    switch(window.orientation)
    {
        // portrait
        case 0:
        case 180:
            // set shareButton x value
            $(".shareButton").css("left", "240px");
        break;
        
        // landscape
        case -90:
        case 90:
            // set shareButton x value
            $(".shareButton").css("left", "390px");
            // set Features menu y values
        break;
    }
}




$(document).ready(function() {
    
    console.log("DOM IS LOADED");
    // set viewport scaling options, web clip icon, and external site css
    $("head").append('<meta name="viewport" content="width=device-width" >');
    $("head").append('<meta name="viewport" content="maximum-scale=1.0; minimum-scale=1.0" >');
    //$("head").append('<link rel="apple-touch-icon" href="" >');
    $("head").append('<link rel="stylesheet" type="text/css" href="http://feed.qwapi.com/static/iphone/clients/adage/newadage.css" >');
    $("body").css("-webkit-text-size-adjust", "none");
    $("body").attr("onorientationchange", "updateOrientation();");
    
    // hide this div class for cases where the E4X sitserver code doesn't handle it
    $(".search-sponsors").hide();
    
    // scroll to get rid of addressbar
    //window.scrollTo(0,1);
    
    // turn off the ajaxy ads
    // load the banner ad
    //banURL = $("#adData").attr("ban");
    //cpcURL = $("#adData").attr("cpc");
    //$("#bannerAdFull").load(banURL);        
    // do the setTimeout for Banner here
    //doSetTimeout();
    
    // Handle what happens when you click on the 3Minute Ad Age div
    $("#3MAAFeature").click(function(){
        //alert($(this).attr("href"));
        var u = $(this).attr("id");
        var dest = "";
        dest = "http://site.qwapi.com/siteserver/site?sid=adage_ip&pid=video";
        document.location.href = dest;  
    });
    
    // set what to do when a feature is clicked on
    $(".adageFeature").click(function(){
        //alert($(this).attr("id"));
        var u = $(this).attr("id");
        var section = $(this).attr("section");
        var sectionURL = $(this).attr("sectionURL");
        var video = $(this).attr("video");
        var blog = $(this).attr("blog");
        //alert(blog);
        var dest = "";
        if(u == "3MAA" || video == "true"){
            dest = "http://site.qwapi.com/siteserver/site?sid=adage_ip&pid=video";
        } else {
            dest = "http://site.qwapi.com/siteserver/site?sid=adage_ip&pid=article&targetURL=" + encodeURIComponent(u) + "&section=" + encodeURIComponent(section) + "&sectionURL=" + encodeURIComponent(sectionURL) + "&blog=" + blog;
        }
        document.location.href = dest;
    });

    // set what to do when a search is clicked on
    $("#searchButton").click(function(){
        var phrase = $("#search").val();
        //alert(phrase);
        var dest = "http://site.qwapi.com/siteserver/site?sid=adage_ip&pid=search&phrase=" + encodeURIComponent(phrase);
        document.location.href = dest;
    });


    $(".navBar").click(function(){
        var section = $(this).attr("section");
        var sectionURL = $(this).attr("id");
        if(sectionURL){
           console.log("SECTION: " + sectionURL);
           //var link = "http://adage.com/" + section;
           var dest = "";
           dest = "http://site.qwapi.com/siteserver/site?sid=adage_ip&pid=section&targetURL=" + encodeURIComponent(sectionURL) + "&section=" + encodeURIComponent(section);
           document.location.href = dest;
        } else {
            console.log("NO SECTION");
        }
    });

    // set what to do when a search is clicked on
       $(".navCell").click(function(){
           var id = $(this).attr("id");
           var dest = "";
           dest = "http://site.qwapi.com/siteserver/site?sid=adage_ip&pid=section&targetURL=" + encodeURIComponent(id) + "&section=Latest%20News";
           if(id == "video"){
               dest = "http://site.qwapi.com/siteserver/site?sid=adage_ip&pid=video";
           }
           if(id == "more"){
               if($("#tapNav").is(":visible") || $(".sectionNav").is(":visible") || $(".sectonDiv").is(":visible")){
                   hideTapNav();
                   $(".sectionNav").hide();
                   $(".sectionDiv").hide();
                   
               } else {
                   showTapNav();
               }
               return;
           }
           document.location.href = dest;
       });



    // set what to do when a menu item is clicked on
    $(".menuCell").click(function(){
        //alert($(this).attr("id"));
        if($(this).attr("id") == "thisWeek"){
            $(".sectionDiv").hide();
            $(".adageFeature").hide();
            $("#thisWeekCats").hide();
            $("#moreCats").hide();
            $("#thisWeekDiv").show();
        }
        if($(this).attr("id") == "home"){
            $(".sectionDiv").hide();
            $("#thisWeekDiv").hide();
            $("#thisWeekCats").hide();
            $("#moreCats").hide();
            $(".adageFeature").show();
        }
        
        if($(this).attr("id") == "more"){
            $(".sectionDiv").hide();
            $("#thisWeekDiv").hide();
            $("#thisWeekCats").hide();
            $(".adageFeature").hide();
            $("#moreCats").show();
        }
    });
    /*
    $("#coverImage").click(function(){
       $(".sectionDiv").hide();
       $("#thisWeekDiv").hide();
       $("#thisWeekCats").show(); 
    });*/
    
    
    // Handle Section clicks
    $(".section").click(function(){
       var id = $(this).attr("id");
       var dest = "";
       if(id == "Events" ){
           dest = "http://site.qwapi.com/siteserver/site?sid=adage_ip&pid=section&targetURL=http://feeds.feedburner.com/AdvertisingAge/Events&blog=true&section=AdAge%20Events";
           document.location.href = dest;
       } else if(id == "IndEvents"){
           dest = "http://site.qwapi.com/siteserver/site?sid=adage_ip&pid=section&targetURL=http://feeds.feedburner.com/AdvertisingAge/IndustryEvents&blog=true&section=Industry%20Events";
           document.location.href = dest;
       } else {
           $("#tapNav").hide();
           $(".sectionNav").show();
           divID = "#sectionDiv" + id;
           $(divID).show();
       }
    });
    
    // handle article link clicks
    $(".articleLink").click(function(){
        //alert($(this).attr("id"));
        var u = $(this).attr("id");
        if(u.search(/http/) == -1){
            u = "http://www.adage.com/" + u;
        }
        var dest = "http://site.qwapi.com/siteserver/site?sid=adage_ip&pid=article&targetURL=" + encodeURIComponent(u);
        document.location.href = dest;
    });
    
    // handle department link clicks
    $(".deptLink").click(function(){
           //alert($(this).attr("id"));
           var u = $(this).attr("id");
           var section = $(this).attr("section");
           var blog = $(this).attr("blog");
           var dest = "http://site.qwapi.com/siteserver/site?sid=adage_ip&pid=section&targetURL=" + encodeURIComponent(u) + "&section=" + encodeURIComponent(section) + "&blog=" + blog;
           document.location.href = dest;
     });
    
    // handle misc navigation
    $(".backNav").click(function(){
        $(".sectionDiv").hide();
        $("#thisWeekDiv").hide();
        $("#thisWeekCats").show();
    });
    $(".backNavFeatures").click(function(){
        $(".sectionDiv").hide();
        $("#regFeatures").show();
    });
    
});


