/*
*************************************************

SUPEREST
JavaScript Functions (requires jQuery)

Created by the ridiculous Dan Mall
http://www.danielmall.com/

*************************************************
*/


/*-------------------------------------------    
    Global Variables
-------------------------------------------*/

var RANDOM_HERO_DELAY = 0.5; // meaured in seconds (ex: 1 = 1 second, 0.5 = half a second, etc)



/*-------------------------------------------    
    Initial Actions
-------------------------------------------*/

function superest() {
    
    //createFlashNav();
    //animateArrows();
    initSubmitRollovers();    
    addCommentPreview();
    initRandomHero();
    initStoreThumbs();
   
}

// this runs EVERYTHING
$(document).ready(function() { superest(); });



/*-------------------------------------------    
    Custom Functions
-------------------------------------------*/

/** 
*   Creates the navigation as a Flash movie
*   
*   @author Dan Mall <dan@danielmall.com>
*   @version 1.0
*   @copyright Dan Mall
*/


function createFlashNav(){
    
    $('#nav').after('<div id="nav-flash"></div>');
    
    var navLinks = new Array();
    $('#nav a').each(function(){
        navLinks.push($(this).attr('href'));
    });
    
    var flashvars = {
        navLinks: navLinks,
        currentURL: window.location
    }
    
    var attributes = {
        id: 'nav-flash',
        name: 'nav-flash'
    }   
     
    swfobject.embedSWF("/Main.swf", "nav-flash", "656", "128", "9.0.0","expressInstall.swf", flashvars, { menu: 'false', wmode: 'transparent'}, attributes);
    
}


/** 
*   Applies rollover animation to "Vanquished" and "Defeated By" arrows
*   
*   @author Dan Mall <dan@danielmall.com>
*   @version 1.0
*   @copyright Dan Mall
*
*/

function animateArrows(){
    
    $('#superest-nav').append('<div id="up-arrow"></div><div id="down-arrow"></div>');
    
    // event handlers
    $('#vanquished a').mouseenter(function(){
        $('#down-arrow').animate({
            backgroundPosition: '0px 0px'
        }, 150, 'swing');
    }).mouseleave(function(){
        $('#down-arrow').animate({
            backgroundPosition: '0px -22px'
        }, 150, 'swing');
    });
    
    $('#defeated-by a').mouseenter(function(){
        $('#up-arrow').animate({
            backgroundPosition: '0px -22px'
        }, 150,'swing');
    }).mouseleave(function(){
        $('#up-arrow').animate({
            backgroundPosition: '0px 0px'
        }, 150, 'swing');
    });
    
}


/** 
*   Applies rollover animation to Submit buttons
*   
*   @author Dan Mall <dan@danielmall.com>
*   @version 1.0
*   @copyright Dan Mall
*
*/

function initSubmitRollovers(){
    
    $('#find-hero').mouseover(function(){
        $(this).attr('src', '/_img/_structure/_buttons/find-hero-over.gif');
    }).mouseout(function(){
        $(this).attr('src', '/_img/_structure/_buttons/find-hero.gif');
    });
    
    $('#purchase').mouseover(function(){
        $(this).attr('src', '/_img/_structure/_buttons/buy_button_over.gif');
    }).mouseout(function(){
        $(this).attr('src', '/_img/_structure/_buttons/buy_button.gif');
    });
    
    
}


/** 
*   JavaScript alerts a message from Flash
*   
*   @author Dan Mall <dan@danielmall.com>
*   @version 1.0
*   @copyright Dan Mall
*
*/

function outputMessage(s){
	alert(s);
}

/** 
*   Thumb Swapping - Added by Kevin.
*   "newimg" is the image's name minus its file extension.
*/

function swap(id, newimg) {
	var me = document.getElementById(id);	
	me.src = newimg;
}
function clear() {
	document.getElementById('thumb1').src = 'thumb_01.jpg';
	document.getElementById('thumb2').src = 'thumb_02.jpg';
	document.getElementById('thumb3').src = 'thumb_03.jpg';
}


/** 
*   Adds a real-time comment preview to comment forms
*   
*   @author Dan Mall <dan@danielmall.com>
*   @version 2.0
*   @copyright Dan Mall
*
*/

function addCommentPreview(){
    
    $('.aftermath-comments').append('<li id="comment-preview"></li>');
    
    $('#comment-author, #url, #text').keyup(function(){
        populateComment();
    });
    
    //initial run
    populateComment();
    
}

function populateComment(){
    
    if($('.aftermath-comments').size() > 0){

        var commentContents = '';
    
        // start populating textarea
        if($('#text').val() != ''){
            commentContents += '<p>' + $('#text').val().replace(/\n/g,'<br />') + '</p>';        
        }else{
            commentContents += '<p id="initial-message">Your comment will go here once you start typing. Won&rsquo;t that be nice?</p>';
        }
    
        // citation
        if($('#comment-author').val() != ''){
        
            // if the user has typed a URL...
            if($('#url').val() != ''){
            
                // if the URL doesn't contain 'http://'...
                if($('#url').val().indexOf('http://') == -1){
                    commentContents += '<p><cite><a href="http://' + $('#url').val() + '">' + $('#comment-author').val() + '</a></cite></p>';
                }else{
                    commentContents += '<p><cite><a href="' + $('#url').val() + '">' + $('#comment-author').val() + '</a></cite></p>';
                }            
            }else{
                commentContents += '<p><cite>' + $('#comment-author').val() + '</cite></p>';
            }
        }else{
            // if the user has typed a URL...
            if($('#url').val() != ''){
            
                // if the URL doesn't contain 'http://'...
                if($('#url').val().indexOf('http://') == -1){
                    commentContents += '<p><cite><a href="http://' + $('#url').val() + '">(name value goes here)</a></cite></p>';
                }else{
                    commentContents += '<p><cite><a href="' + $('#url').val() + '">(name value goes here)</a></cite></p>';
                }            
            }else{
                commentContents += '<p><cite>(name value goes here)</cite></p>';
            }
        }
    
    
        $('#comment-preview').html(commentContents); 
    }
}


/** 
*   Adds Ajax reloading functionality to Random Hero module
*   
*   @author Dan Mall <dan@danielmall.com>
*   @version 2.0
*   @copyright Dan Mall
*
*/

function initRandomHero(){
    
    var animating = false;
    
    $('#random h3:eq(0)').after('<ul id="random_buttons"><li><a id="roll_again" href="#random">Roll Again</a></li><li><a id="view_hero" href="' + $('#random h4 a:eq(0)').attr('href') + '">View Hero</a></li></ul>');
    
    // Roll Again functionality
    $('#roll_again').click(function(){
        
        if(!animating){
            
            animating = true;
        
            // add spinner
            $(this).before('<div id="spinner"><img src="/_img/_structure/ajax-loader.gif" alt="Loading" /></div>');
            $('#spinner').css({'opacity': 0}).animate({'opacity': 1},'normal');
        
            // create a new .hero-entry and dump all the contents of the new hero in there
            $('#random .hero-entry:eq(0)').after('<div class="hero-entry"></div>');
            $('#random .hero-entry:eq(1)').css({
                                              'opacity': 0, 
                                              'position': 'absolute',
                                              'top': '143px'
                                              })
                                          .load('/_inc/random_hero.php .hero-entry > *', function(){
                                          
                                              // change the link on the "View Hero" button
                                              $('#view_hero').attr('href', $(this).find('h4 a:eq(0)').attr('href'));
                                          
                                              // animate in
                                              $(this).delay((RANDOM_HERO_DELAY*1000)).animate({'opacity': 1}, 'normal', function(){
                                                  $(this).css({'position':'static'}).attr();
                                              });
                                              $('#random .hero-entry:eq(0)').delay((RANDOM_HERO_DELAY*1000)).fadeOut('normal', function(){
                                                  $(this).remove();
                                                  $('#spinner').fadeOut('fast', function(){
                                                     $(this).remove(); 
                                                     animating = false;
                                                  });
                                              });
                                          });
                    
        }
        
        return false;
        
    });
    
}


/** 
*   Adds slick thumbnail switching to store thumbnails
*   
*   @author Dan Mall <dan@danielmall.com>
*   @version 2.0
*   @copyright Dan Mall
*
*/

function initStoreThumbs(){
    
    // add pointer
    $('#product-thumbs').append('<div id="pointer"></div>');
    
    // assign position variables to each link
    $('#product-thumbs a:eq(0)').attr('left', 55);
    $('#product-thumbs a:eq(1)').attr('left', 196);
    $('#product-thumbs a:eq(2)').attr('left', 336);
    
    $('#product-thumbs a').click(function(){
        
        // load proper image
        $('#product-image').append('<img src="' + $(this).attr('href') + '" />');
        
        // find added image, fade in, then remove previous image
        $('#product-image img').delay(500).last().hide().fadeIn('normal', function(){
            $(this).prev().remove();            
        });
        
        movePointer($(this).attr('left'));
        
        return false;
        
    });
    
}

function movePointer($left){
      $('#pointer').animate({
          left: $left
      }, 500);  
}


