// Preload images
function addbuttonimagenames(array, rootname) {
    array.push(rootname + 'Normal.png');
    array.push(rootname + 'Pressed.png');
    array.push(rootname + 'Rollover.png');
}

var imgObjects = [];
function loadimages(names) {
    for (var i = imgObjects.length; i < names.length; i++) {
        imgObjects[i] = new Image(); 
        imgObjects[i].src = 'images/' + imgNames[i];
    }  
}

var imgNames = ['SMWBackground.jpg', 'SMWCreditsBackground.jpg'];
addbuttonimagenames(imgNames, 'SMWBuyButton');
addbuttonimagenames(imgNames, 'SMWCreditsButton');
addbuttonimagenames(imgNames, 'SMWNavigationHome'); 
addbuttonimagenames(imgNames, 'SMWNavigationScreenshots'); 
addbuttonimagenames(imgNames, 'SMWNavigationVideos'); 
addbuttonimagenames(imgNames, 'SMWNavigationSupport'); 
imgNames.push('SMWScreenshotsText.png');
imgNames.push('SMWScreenshotsArrowLeft.png');
imgNames.push('SMWScreenshotsArrowRight.png');
for (var i = 1; i <= 8; i++) {
    imgNames.push('SMWScreenshotThumbnail' + String(i) + '.png');
}
imgNames.push('SMWVideoThumbnail1.png');
imgNames.push('SMWVideosText.png');
loadimages(imgNames);

// Main navigation
var navmoveduration = 0.3;
var displayedcontent = 'hometext';

function swapcontent(newcontent) {
    if ( newcontent == displayedcontent ) return;
    Effect.Fade(displayedcontent)
    displayedcontent = newcontent;
    Effect.Appear(displayedcontent);
}

function selecthomenavitem() {
    new Effect.Move('navigationpointer', { x: 180, y: 95, mode:'absolute', duration:navmoveduration});
    swapcontent('hometext');
}

function selectscreenshotsnavitem() {
    new Effect.Move('navigationpointer', { x: 245, y: 157, mode:'absolute', duration:navmoveduration});
    swapcontent('screenshots');
    loadimages(['iPhoneShell.png']);
}

function selectvideosnavitem() {
    new Effect.Move('navigationpointer', { x: 190, y: 213, mode:'absolute', duration:navmoveduration});
    swapcontent('videos');
}

function selectsupportnavitem() {
    new Effect.Move('navigationpointer', { x: 200, y: 277, mode:'absolute', duration:navmoveduration});
    swapcontent('support');
}

// Darken screen
function grayout(vis, options) {
    // Pass true to gray out screen, false to ungray
    // options are optional.  This is a JSON object with the following (optional) properties
    // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
    // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
    // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
    // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
    // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
    // in any order.  Pass only the properties you need to set.
    var options = options || {}; 
    var zindex = options.zindex || 50;
    var opacity = options.opacity || 70;
    var opaque = (opacity / 100);
    var bgcolor = options.bgcolor || '#000000';
    var dark=document.getElementById('darkenscreenobject');
    if (!dark) {
        // The dark layer doesn't exist, it's never been created.  So we'll
        // create it here and apply some basic styles.
        // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
        var tbody = document.getElementsByTagName("body")[0];
        var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenscreenobject';                   // Name it so we can find it later
        tbody.appendChild(tnode);                            // Add it to the web page
        dark=document.getElementById('darkenscreenobject');  // Get the object.
    }
    
    if (vis) {
        // Calculate the page width and height 
        if ( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
            var pageWidth = document.body.scrollWidth+'px';
            var pageHeight = document.body.scrollHeight+'px';
        } 
        else if( document.body.offsetWidth ) {
          var pageWidth = document.body.offsetWidth+'px';
          var pageHeight = document.body.offsetHeight+'px';
        } 
        else {
           var pageWidth='100%';
           var pageHeight='100%';
        }   

        //set the shader to cover the entire page and make it visible.
        dark.style.opacity=opaque;                      
        dark.style.MozOpacity=opaque;                   
        dark.style.filter='alpha(opacity='+opacity+')'; 
        dark.style.zIndex=zindex;        
        dark.style.backgroundColor=bgcolor;  
        dark.style.width= pageWidth;
        dark.style.height= pageHeight;

        Effect.Appear(dark, {to:0.7, duration:1.3});             
    } 
    else {
        Effect.Fade(dark, {duration:0.5});
    }
}

// Screenshot navigation
var widthofthumbnail = 180;
var screenshotindex = 0;
var numberofscreenshots = 8;
var numberofonscreenshots = 4;

function movetoprevious() {
    screenshotindex--;
    new Effect.Move('innerscreenshots', 
        { x: -screenshotindex*widthofthumbnail, y: 0, mode:'absolute', duration:0.5, transition: Effect.Transitions.sinoidal });
    if ( screenshotindex <= 0 ) {
        $('screenshotsleftarrow').style.visibility = 'hidden';
    }    
    $('screenshotsrightarrow').style.visibility = 'visible';
}  

function movetonext() {
    screenshotindex++;
    new Effect.Move('innerscreenshots', 
        { x: -screenshotindex*widthofthumbnail, y: 0, mode:'absolute', duration:0.5, transition: Effect.Transitions.sinoidal });
    if ( screenshotindex > numberofscreenshots - numberofonscreenshots - 1 ) {
        $('screenshotsrightarrow').style.visibility = 'hidden';
    }    
    $('screenshotsleftarrow').style.visibility = 'visible';
}

// Screenshot Display
function displayscreenshot(source, imagename) {
    grayout(true);
    Effect.Appear('screenshot', {duration:0.5});
    $('screenshot').onclick = hidescreenshot;
    $('screenshotimage').onclick = hidescreenshot;
    $('screenshotimage').setAttribute('src', 'images/' + imagename);
}

function hidescreenshot() {
    grayout(false);
    Effect.Fade('screenshot', {duration:0.5});
    $('darkenscreenobject').onclick = hidescreenshot;
}

// Video Display
function displayvideo(source, imagename) {
    grayout(true);
    // Effect.Appear('video', {duration:0.5});
    setTimeout("$('video').style.display = 'block';", 1500);
    $('video').onclick = hidevideo;
    $('darkenscreenobject').onclick = hidevideo;
}

function hidevideo() {
    document.quicktimeobject.Stop();
    setTimeout('fadeoutvideo();', 1000);
}

function fadeoutvideo() {
    grayout(false);
    $('video').style.display = 'none';
    // Effect.Fade('video', {duration:1.0});
}