var gWebSite = "www.commodoreusa.net";

if (window.location.href == "http://shop.commodoreusa.net/Commodore-C64_c3.htm") window.location.href = "http://" + gWebSite + "/CUSA_C64Select.aspx";
if (window.location.href == "http://shop.commodoreusa.net/Commodore-VIC-Pro_c8.htm") window.location.href = "http://" + gWebSite + "/CUSA_VICProSelect.aspx";
if (window.location.href == "http://shop.commodoreusa.net/Commodore-VIC-Slim_c9.htm") window.location.href = "http://" + gWebSite + "/CUSA_VICSLimSelect.aspx";
if (window.location.href.indexOf("main.sc") > 0) window.location.href = "http://" + gWebSite + "/CUSA_Store.aspx";
if (window.location.href == "http://shop.commodoreusa.net/category.sc?categoryId=1") window.location.href = "http://" + gWebSite + "/CUSA_Store.aspx";

//if (window.location.href == "https://shop.commodoreusa.net/checkoutshipping-submit.sc") window.location.href = "http://" + gWebSite + "/CUSA_Store.aspx";
//if (window.location.href == "https://shop.commodoreusa.net/checkoutaddresses-submit.sc") window.location.href = "http://" + gWebSite + "/CUSA_Store.aspx";
if (window.location.href.indexOf("category.sc") > 0) window.location.href = "http://" + gWebSite + "/CUSA_Store.aspx";

// C64
//if (window.location.href == "http://shop.commodoreusa.net/product.sc?productId=1") window.location.href = "http://" + gWebSite + "/CUSA_C64Select.aspx";
//if (window.location.href == "http://shop.commodoreusa.net/product.sc?productId=2") window.location.href = "http://" + gWebSite + "/CUSA_C64Select.aspx";
//if (window.location.href == "http://shop.commodoreusa.net/product.sc?productId=7") window.location.href = "http://" + gWebSite + "/CUSA_C64Select.aspx";
//if (window.location.href == "http://shop.commodoreusa.net/product.sc?productId=9") window.location.href = "http://" + gWebSite + "/CUSA_C64Select.aspx";
//if (window.location.href == "http://shop.commodoreusa.net/product.sc?productId=10") window.location.href = "http://" + gWebSite + "/CUSA_C64Select.aspx";

// VIC-Pro
//if (window.location.href == "http://shop.commodoreusa.net/product.sc?productId=3") window.location.href = "http://" + gWebSite + "/CUSA_VICProSelect.aspx";
//if (window.location.href == "http://shop.commodoreusa.net/product.sc?productId=11") window.location.href = "http://" + gWebSite + "/CUSA_VICProSelect.aspx";
//if (window.location.href == "http://shop.commodoreusa.net/product.sc?productId=12") window.location.href = "http://" + gWebSite + "/CUSA_VICProSelect.aspx";

// VIC-Slim
//if (window.location.href == "http://shop.commodoreusa.net/product.sc?productId=8") window.location.href = "http://" + gWebSite + "/CUSA_VICSLimSelect.aspx";
//if (window.location.href == "http://shop.commodoreusa.net/product.sc?productId=13") window.location.href = "http://" + gWebSite + "/CUSA_VICSLimSelect.aspx";
//if (window.location.href == "http://shop.commodoreusa.net/product.sc?productId=14") window.location.href = "http://" + gWebSite + "/CUSA_VICSLimSelect.aspx";

// C64
if (window.location.href.indexOf("shop.commodoreusa.net/product.sc?productId=1") > 0) window.location.href = "http://" + gWebSite + "/CUSA_C64Select.aspx";
if (window.location.href.indexOf("shop.commodoreusa.net/product.sc?productId=2") > 0) window.location.href = "http://" + gWebSite + "/CUSA_C64Select.aspx";
if (window.location.href.indexOf("shop.commodoreusa.net/product.sc?productId=7") > 0) window.location.href = "http://" + gWebSite + "/CUSA_C64Select.aspx";
if (window.location.href.indexOf("shop.commodoreusa.net/product.sc?productId=9") > 0) window.location.href = "http://" + gWebSite + "/CUSA_C64Select.aspx";
if (window.location.href.indexOf("shop.commodoreusa.net/product.sc?productId=10") > 0) window.location.href = "http://" + gWebSite + "/CUSA_C64Select.aspx";

// VIC-Pro
if (window.location.href.indexOf("shop.commodoreusa.net/product.sc?productId=3") > 0) window.location.href = "http://" + gWebSite + "/CUSA_VICProSelect.aspx";
if (window.location.href.indexOf("shop.commodoreusa.net/product.sc?productId=11") > 0) window.location.href = "http://" + gWebSite + "/CUSA_VICProSelect.aspx";
if (window.location.href.indexOf("shop.commodoreusa.net/product.sc?productId=12") > 0) window.location.href = "http://" + gWebSite + "/CUSA_VICProSelect.aspx";

// VIC-Slim
if (window.location.href.indexOf("shop.commodoreusa.net/product.sc?productId=8") > 0) window.location.href = "http://" + gWebSite + "/CUSA_VICSLimSelect.aspx";
if (window.location.href.indexOf("shop.commodoreusa.net/product.sc?productId=13") > 0) window.location.href = "http://" + gWebSite + "/CUSA_VICSLimSelect.aspx";
if (window.location.href.indexOf("shop.commodoreusa.net/product.sc?productId=14") > 0) window.location.href = "http://" + gWebSite + "/CUSA_VICSLimSelect.aspx";



new Image().src = 'j/MenuButton_Commodore_RO.jpg';
new Image().src = 'j/MenuButton_Products_RO.jpg';
new Image().src = 'j/MenuButton_Software_RO.jpg';
new Image().src = 'j/MenuButton_News_RO.jpg';
new Image().src = 'j/MenuButton_AboutUs_RO.jpg';
new Image().src = 'j/MenuButton_Corporate_RO.jpg';
new Image().src = 'j/MenuButton_Amiga_RO.jpg';

new Image().src = 'j/MB.jpg';
new Image().src = 'j/MB_RO.jpg';
new Image().src = 'j/buyonline2Highlight.gif';

var TimeToFade = 2000.0;
var interupt = 33;

function fade(eid, override, fadespeed, opacity)
{
    var element = document.getElementById(eid);
    if(element == null) return;

    if(fadespeed == undefined){
	    fadespeed = TimeToFade;
    }

    if(override != undefined){
	    if(override) element.FadeState = -2 
        else element.FadeState = 2;
    }else{

        if(element.FadeState == null)
        {
            if(element.style.opacity == null
                || element.style.opacity == ''
                || element.style.opacity == '1'){
                element.FadeState = 2;
            }else{
                element.FadeState = -2;
            }
        }
    }
  
    // Is animation currently in progress? 
    if(element.FadeState == 1 || element.FadeState == -1){
        element.FadeState = element.FadeState == 1 ? -1 : 1;
        element.FadeTimeLeft = fadespeed - element.FadeTimeLeft;
    }else{
        element.FadeState = element.FadeState == 2 ? -1 : 1;
        element.FadeTimeLeft = fadespeed;
        setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "'," + opacity + ")", interupt);
    }  
}

function animateFade(lastTick, eid, finishOpacity){  
    var curTick = new Date().getTime();
    var elapsedTicks = curTick - lastTick;
 
    var element = document.getElementById(eid);
 
    if(element.FadeTimeLeft <= elapsedTicks) {
        element.style.opacity = element.FadeState == 1 ? '1' : '0';
        if (finishOpacity != undefined) {
            element.style.opacity = element.FadeState == 1 ? "0." + finishOpacity : '0';
            element.style.filter = 'alpha(opacity = ' + (element.FadeState == 1 ? '85' : '0') + ')';
        } else {
            element.style.filter = "alpha(opacity = " + (element.FadeState == 1 ? + finishOpacity : "0") + ")";
        }

        element.FadeState = element.FadeState == 1 ? 2 : -2;
        return;
    }
 
    element.FadeTimeLeft -= elapsedTicks;
    var newOpVal = element.FadeTimeLeft/TimeToFade;
    if(element.FadeState == 1) newOpVal = 1 - newOpVal;

    element.style.opacity = newOpVal;
    if (finishOpacity != undefined) {
        element.style.opacity = newOpVal * (finishOpacity / 100);
        element.style.filter = 'alpha(opacity = ' + finishOpacity + ')';
    } else {
        element.style.filter = 'alpha(opacity = ' + (newOpVal * 100) * 0.85 + ')';
    }

    //element.style.filter = 'alpha(opacity = ' + (newOpVal * 100) * 0.85 + ')';

    setTimeout("animateFade(" + curTick + ",'" + eid + "'," + finishOpacity + ")", 33);
}

var t;
var timer_is_on=0;
var v=1;
var x=0;
var limit=2;

var timeCount = 7000;

function timedCount(){
    if (limit == 2) {
        fade("pic1", v==1);
        fade("pic2", v==2);
    }

    if(limit==3){
        if(v!=3) fade("pic1", v==1);
        if(v!=1) fade("pic2", v==2);
        if(v!=2) fade("pic3", v==3);
    }

    if (limit == 5) {
        if (v == 1 || v == 2) fade("pic1", v == 1);
        if (v == 2 || v == 3) fade("pic2", v == 2);
        if (v == 3 || v == 4) fade("pic3", v == 3);
        if (v == 4 || v == 5) fade("pic4", v == 4);
        if (v == 5 || v == 1) fade("pic5", v == 5);
    }

    if (limit == 6) {
        if (v == 1 || v == 2) fade("pic1", v == 1);
        if (v == 2 || v == 3) fade("pic2", v == 2);
        if (v == 3 || v == 4) fade("pic3", v == 3);
        if (v == 4 || v == 5) fade("pic4", v == 4);
        if (v == 5 || v == 6) fade("pic5", v == 5);
        if (v == 6 || v == 1) fade("pic6", v == 6);
    }
    //alert(v);

//fade("pic4", v==4);

//if(limit>=5&&x==4) fade("pic5", v==5);
    t = setTimeout("timedCount()", timeCount);
    x=v;
    v++;
    if(v==(limit+1)) v=1;
}

function startSlideShow(slideshows){
    if(slideshows==undefined){
        limit=2;
    }else{
        limit=slideshows;
    }
//limit++;

    for(var i=1;i<=limit;i++) {
        show("pic" + i);
    }

    if (!timer_is_on) {
        timer_is_on=1;
        timedCount();
    }
}

function SetOpacity(objName, opacity){
    var o=document.getElementById(objName);  
    if(o!=null&&o!=undefined) setOpacity(o, opacity);
}

function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	// IE/Win
	obj.style.filter = "alpha(opacity='"+ opacity +"')";

	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

function hideSubMenu(){
    var o=document.getElementById("ProductsMenu");  
    if(o!=null&&o!=undefined) o.style.display='none';
    var o = document.getElementById("SoftwareMenu");
    if (o != null && o != undefined) o.style.display = 'none';
    var o=document.getElementById("AboutUsMenu");  
    if(o!=null&&o!=undefined) o.style.display='none';
    var o = document.getElementById("StoreMenu");
    if (o != null && o != undefined) o.style.display = 'none';
    menustarted = false;
}

var menustarted = false;

function showSubMenu(name){
    var o=document.getElementById(name);  
    if(!menustarted){
        SetOpacity(name, 0);
    }
    if(o!=null&&o!=undefined) o.style.display='block';

    if(!menustarted){
        //fade(name, true, 500, 80);
        fade(name, true, 500);
        menustarted = true;
    }

}

function mon(o){
    o.style.background = 'url(http://' + gWebSite + '/j/MB_RO.jpg)';
    //o.style.background = 'url(/j/MB_RO.jpg)';
}

function mout(o){
    o.style.background = 'url(http://' + gWebSite + '/j/MB.jpg)';
    //o.style.background = 'url(/j/MB.jpg)';
}

function iego(s) {
    var iev = IEVersion();
    if (iev == "NA") return;
    //if (iev < 8) {
        //alert(s);
    //go("http://" + gWebSite + "/" + s);
    go(s);
    //}
}

function go(s) {
    s = s.replace("(Website)", gWebSite);
    window.location.href = s;
}

function hide(name){
    var o;
    o=document.getElementById(name);
    if(o!=null&&o!=undefined) o.style.display='none';
}

function show(name){
    var o;
    o=document.getElementById(name);
    if(o!=null&&o!=undefined) {
        o.style.display='block';
        o.style.filter = 'alpha(opacity = ' + (o.FadeState == 1 ? '85' : '0') + ')';
    }
}

var lastTabName = "Design";

function tab(name){
    var o;

    if (name == lastTabName) return;

    if(name!=lastTabName) hide("Design");
    if(name!=lastTabName) hide("Technology");  
    if(name!=lastTabName) hide("Specifications");  
    if(name!=lastTabName) hide("Resources");
    if(name!=lastTabName) hide("Configurations");  

    // Fade out the last tab.
    if(lastTabName!=name){
        fade(lastTabName, false, 1000, 100);
        show(name);  
    }

    // Fade in the current tab.
    fade(name, true, 1000);

    // Swith the tab graphics.
    o=document.getElementById("tabDesign");
    if (o != null && o != undefined) o.src = 'j/webtab_design.gif';
    o=document.getElementById("tabTechnology");
    if (o != null && o != undefined) o.src = 'j/webtab_technology.gif';
    o=document.getElementById("tabSpecifications");
    if (o != null && o != undefined) o.src = 'j/webtab_specifications.gif';
    o=document.getElementById("tabResources");
    if (o != null && o != undefined) o.src = 'j/webtab_resources.gif';
    o = document.getElementById("tabConfigurations");
    if (o != null && o != undefined) o.src = 'j/webtab_configurations.gif';

    o=document.getElementById("tab" + name);
    if (o != null && o != undefined) { o.src = 'j/webtab_' + name + '_sel.gif' };

    lastTabName = name;
}

//var lastToggleName = "";
// Used in News.
function toggle(name) {
    //if (name == lastToggleName) return;
    //lastToggleName = name;

    o = document.getElementById(name);
    if (o != null && o != undefined) {
        if (o.style.display == 'block') {
            hide(name);
            setOpacity(o, 1);

        } else {
            if (!document.all) fade(name, true, 1000);
            show(name);
            if (document.all) setOpacity(o, 125);
        }
    }
}

function expand(){

}
function shrink(){

}

function translate() {
    /*alert('HELLO');*/
    var transValue = document.getElementById("TransValue").value;
    alert("transValue=" + transValue + "  loc=" + location.href.indexOf("google"));
    if (transValue != "" && !(location.href.indexOf("google") > 0) ) {
        //location.href = "http://translate.googleusercontent.com/translate_c?hl=en&ie=UTF8&langpair=" + transValue + "&u=" + location.href;
        location.href = "http://www.google.com/translate?hl=en&ie=UTF8&langpair=" + transValue + "&u=" + location.href;
    }
}

function toggleFlags() {
    if (location.href.indexOf("google") > 0) {
        document.getElementById("Flags").style.display = "none";
        document.getElementById("English").style.display = "block";
    } else {
        document.getElementById("Flags").style.display = "block";
        document.getElementById("English").style.display = "none";
    }
}

// IE8's "Compatibility mode" is anything but.  Oh well, at least it doesn't take 40 lines of code to detect and work around it.
// Oh wait:
/*
* Author: Rob Reid
* CreateDate: 20-Mar-09
* Description: Little helper function to return details about IE 8 and its various compatibility settings either use as it is
* or incorporate into a browser object. Remember browser sniffing is not the best way to detect user-settings as spoofing is
* very common so use with caution.
*/
function IEVersion() {
    var _n = navigator, _w = window, _d = document;
    var version = "NA";
    var na = _n.userAgent;
    var ieDocMode = "NA";
    var ie8BrowserMode = "NA";
    // Look for msie and make sure its not opera in disguise
    if (/msie/i.test(na) && (!_w.opera)) {
        // also check for spoofers by checking known IE objects
        if (_w.attachEvent && _w.ActiveXObject) {
            // Get version displayed in UA although if its IE 8 running in 7 or compat mode it will appear as 7
            version = (na.match(/.+ie\s([\d.]+)/i) || [])[1];
            // Its IE 8 pretending to be IE 7 or in compat mode        
            if (parseInt(version) == 7) {
                // documentMode is only supported in IE 8 so we know if its here its really IE 8
                if (_d.documentMode) {
                    version = 8; //reset? change if you need to
                    // IE in Compat mode will mention Trident in the useragent
                    if (/trident\/\d/i.test(na)) {
                        ie8BrowserMode = "Compat Mode";
                        // if it doesn't then its running in IE 7 mode
                    } else {
                        ie8BrowserMode = "IE 7 Mode";
                    }
                }
            } else if (parseInt(version) == 8) {
                // IE 8 will always have documentMode available
                if (_d.documentMode) { ie8BrowserMode = "IE 8 Mode"; }
            }
            // If we are in IE 8 (any mode) or previous versions of IE we check for the documentMode or compatMode for pre 8 versions            
            ieDocMode = (_d.documentMode) ? _d.documentMode : (_d.compatMode && _d.compatMode == "CSS1Compat") ? 7 : 5; //default to quirks mode IE5                               
        }
    }

//    return {
//        "UserAgent": na,
//        "Version": version,
//        "BrowserMode": ie8BrowserMode,
//        "DocMode": ieDocMode
//    }
    return version;
}
function setup() {
//    var o = document.getElementById("IEBreak");
//    if(o==undefined) return;
//    var iev = IEVersion();
//    if(iev == "NA" || iev > 7) o.style.display = 'none';
}

function iefix() {
    var iev = IEVersion();
    if (iev == "NA") return;
    if (iev < 8) {
        //alert(s);
        //document.scrol;
    }
}

function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [scrOfX, scrOfY];
}

function GetWindowWidth(){
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}
function GetWindowHeight() {
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myHeight = document.body.clientHeight;
    }
    return myHeight;
}
