var app={
    getWMURL:function(url){location.href=url+"?wm="+siteConfig.currentWM},
    switchLang:function(event){
        if(siteConfig.excludeLangPath!="all"){
            event.preventDefault();
            lang=$(this).attr("href").replace("http://","").replace("/","").replace("index.html","").replace("default.html","");
            utils.setDefaultCookie('LCID',lang);
            location.href='http://'+lang+((siteConfig.excludeLangPath!="null"&&siteConfig.excludeLangPath.indexOf(lang.substring(0,2))>-1)||location.pathname.substring(0,6)=='/index'||location.pathname.substring(0,8)=='/default'||lang.indexOf(siteConfig.domain)==-1?'/':location.pathname);
        }else return true;
    },
    dropdownNavigation:function(id,callback){
        var t=$(id);
        if(t.size()>0){
            $("ul li:last-child",t).addClass('last');
            $("ul li:first-child",t).addClass('first').show().siblings().hide();
            $(".first",t).hover(function(){$('li:not(.first)',t).stop(false,true).fadeIn(500)});
            t.hover(function(){},function(){$('li:not(.first)',t).stop(false,true).slideUp(250)});
            if(typeof(callback)=="function")$("a",t).click(callback);
        }
    },
    setHeightToParent:function(div,parentDiv){
        var tmpDiv,tmpParentDiv="";
        if(div&&parentDiv){
            tmpDiv=div;
            tmpParentDiv=parentDiv;
            $(tmpDiv).css('height',function(){return $(tmpParentDiv).css('height')});
        }else if(!parentDiv){
            alert("Parent Not Set. Function Usage: app.setHeightToParent('#element','#parent');");
        }else{
            alert("Element & Parent Not Set. Function Usage: app.setHeightToParent('#element','#parent');");
        }
    },
    listStep:function(arg1,arg2){
        if(arg1){
            if(!arg2)arg2="item";
            $(arg1).last().addClass('last').end().first().addClass('first').end().each(function(i){i=i+1;$(this).addClass(arg2+i)});
        }
    },
    launchDownload:function(url,nored){
       function downloadpage(){
           setTimeout(downloadpage,5000);
           location.href=(typeof(url)!="undefined"&&url!="")?url:"/download.html";
       }
       location.href=siteConfig.clientUrl;
       if((siteConfig.clientUrl.indexOf('/Downloads/')>-1||siteConfig.spawnExe)&&typeof(nored)=="undefined")setTimeout(downloadpage,5000);
    },
    attachDownload:function(custom){
        if(siteConfig.clientUrl!="null"&&siteConfig.spawnExe){
            $(typeof(custom)!="undefined"?custom:"a[href*='/download/'],a[href*='/download.html'],a.iedl,.iedl a").live('click',function(e){
                e.preventDefault();
                if($(this).hasClass('noredirect'))app.launchDownload($(this).attr('href'),false);
                else app.launchDownload($(this).attr('href'));
            });
        }
    },
    addTableTrClasses:function(){$('#main table').each(function(){$(this).find("tr:has('td'):odd").addClass('highlight')})},
    popupLink:function(){
        $("a[class*='popupLink'],div[class*='popupLink'] a").click(function(e){
            e.preventDefault();
            var a=typeof($(this).attr('class')!="undefined")&&$(this).attr('class').indexOf('popupLink')>-1?$(this).attr('class'):$(this).parent().attr('class');
            var t=a.slice(a.indexOf('popupLink'),a.length);
            if(t.indexOf(' ')>-1)t=t.slice(0,t.indexOf(' '));
            var c=t.split('-');
            app.openPopup($(this).attr('href'),((typeof(c[3])!='undefined'&&c[3]=='scroll')||(typeof(c[5])!='undefined'&&c[5]=='scroll')?'yes':'no'),c[1],c[2],(typeof(c[3])=="undefined"?0:c[3]),(typeof(c[4])=="undefined"?0:c[4]));
        });
    },
    openPopup:function(url,scrollbar,width,height,t,l){
        if(t==0)t=$(window).height()/2-height/2;
        if(l==0)l=$(window).width()/2-width/2;
        var popupwindow=window.open(url,'popup','scrollbars='+scrollbar+',width='+width+',height='+height+',top='+t+',left='+l);
        popupwindow.focus();
    },
    stripLeftNavTarget:function(){
        $(".page-navigation li a[target='_blank']").attr("target","");
    },
    installStart:function(){
        window.opener='M';
        window.open('','_parent','');
        window.close();
    },
    redirectDetected:false, 
    redirect: function(currentPath, redirectPath, crossPlatform) {
        if ((location.pathname.indexOf(currentPath)>-1) && (typeof(CQ) == "undefined") && location.pathname!=redirectPath && !app.redirectDetected) {
            if (siteConfig['isMac'] && (typeof(crossPlatform) == "undefined")){
                location.href=redirectPath;
                app.redirectDetected=true;
            } else if (crossPlatform == "true") { location.href=redirectPath;app.redirectDetected=true; }
        }
    },      
    macRedirect: function() { // redundant function. needs to be removed once template has been deployed.
        // if (navigator.appVersion.indexOf("Mac")!=-1){window.location="/instant_play/";}
    },    
    resizeToFullscreen: function() {
        window.resizeTo(window.screen.availWidth,window.screen.availHeight);
        window.moveTo(0,0);
    },
    initSlider: function(targetDiv,imgWidth,vCount,speed){
        var iCount = $("#" + targetDiv + " .slides-content").size(),c=$("#"+targetDiv+" .slider-container"),pos2=vCount;
        //$("#"+targetDiv+" .slider-arrow-right").addClass('active');
        if(iCount>vCount){
            $("#"+targetDiv+" .slider-arrow-right").addClass('active');
            $("#"+targetDiv+" .slider-arrow-right").click(function(){
                c.stop(true,true);
                if(pos2+vCount<=iCount+vCount-1){
                    pos2=pos2+vCount;
                    var pos=parseInt(c.css('left').replace('px','')-(vCount*imgWidth));
                    c.animate({'left':pos+'px'},speed);
                }
                if(pos2+vCount>=iCount+vCount-1){
                    $(this).removeClass('active');
                } else {
                    $(this).addClass('active');
                }
                $("#"+targetDiv+" .slider-arrow-left").addClass('active');
            });
            $("#"+targetDiv+" .slider-arrow-left").click(function(){
                c.stop(true,true);
                if(pos2-vCount>=vCount){
                    pos2=pos2-vCount;
                    c.animate({'left':parseInt(c.css('left').replace('px',''))+(vCount*imgWidth)+'px'},speed);
                }
                if(pos2<=vCount){
                    $(this).removeClass('active');
                }else{
                    $(this).addClass('active');
                }
                $("#"+targetDiv+" .slider-arrow-right").addClass('active');
            });
        }
    }
}    
$('html').addClass("js");

$(function(){
    if(location.pathname.indexOf('installstart.html')>-1)app.installStart();
    app.attachDownload();
    app.dropdownNavigation('#language-navigation',app.switchLang);
    app.dropdownNavigation('#product-navigation');
    app.setHeightToParent('#download-button','#footer-box');
    app.listStep('#regulatory-icons ul li','item');
    app.listStep('#sub-navigation ul li','item');
    app.popupLink();
    app.addTableTrClasses();
    app.stripLeftNavTarget();
});

