/**
 * PShelper.com
 * Base Store Version javascript
 *
 * Notes:
 *
 * When creating text box inputs, always apply both
 * a value and default attribute (the same value)
 *
 * For rollovers use attribute 'hoversrc' to define the
 * rollover image source
 *
 **/
$(document).ready(function(){

/**
 * PSHELPER.COM SLIDESHOW
 */
    //initiate the timer to move to the next one
    slideshowTimer = window.setInterval("swapSlide()",8000);
    i = 1; //iterator for assigning ids to the slides
    controlButtons = new String();

    swapSlide = function(slideId)
    {
        $currentSlide = $('.slideWrapper[rel="active"]');
        $currentSlide.removeAttr('rel').fadeOut(1500).appendTo('#slidesContainer');
        $('.controlButton[rel="'+$currentSlide.attr('id')+'"]').removeClass('activeButton');
        $nextSlide = (slideId) ? $('#'+slideId) : $('.slideWrapper:first');
        $nextSlide.attr({'rel':'active'}).fadeIn(1500);
        $('.controlButton[rel="'+$nextSlide.attr('id')+'"]').addClass('activeButton');
    }
    $('.slideWrapper')
        .each(function(){$(this).attr({'id':'slide'+i});i++;}) //apply a unique id to each slide
        .hide() //hide each slide
        .filter(':first').attr({'rel':'active'}) //select the first slide and apply the active denoter
        .show() //show the first slide
        .parent().children().hover( //when a slide is moused over it must pause, and restart when moused out
            //mouse over slide, pause
            function(){
                window.clearInterval(slideshowTimer);
            },
            //mouse out of slide, resume
            function(){
                slideshowTimer = window.setInterval("swapSlide()",8000);
            }
        )
        .parent().append('<div id="slideControls"></div>');//apply a control div to #slidesContainers
    for(var j=1;j<i;j++)
    {
        controlButtons += '<a href="#" class="controlButton" rel="slide'+j+'">'+j+'</a>';
    }
    $('#slideControls')
        .css({'z-index':10000,'position':'absolute','right':'20px','bottom':'20px'})
        .append(controlButtons)
        .children(':first')
        .addClass('activeButton');
    $('.controlButton').click(function(){
       swapSlide($(this).attr('rel')) ;
    });

/**
 * INPUT TYPE=TEXT HANDLING
 */

	//input textbox handling
	$('input[type="text"]').focus(function(){
        if($(this).val()==$(this).attr('default'))
        {
            $(this).val('');
        }
    }).blur(function(){
        if($(this).val()=='')
        {
            $(this).val($(this).attr('default'));
        }
    });

/**
 * BASE TEMPLATE TOP NAVIGATION HANDLING
 */
    //top nav menu handling
	$('#topNavMenu').insertAfter('#mainWrapper'); //<=IE6 fix, #topNavMenu must be absolute to the body
    $('ul.parent li').hover(
        //mouse over
        function(){
           $(this).find('ul.sub').css({'zIndex':9999}).show();
        },
        //mouse out
        function(){
           $(this).find('ul.sub').hide();
        }).each(function(){
            $(this).find('ul.sub').hide();
        });
/**
 * ROLLOVER IMAGE HANDLING
 */
     //rollover image handling
     $preload = new Array();
     $iterator = 0;
     xOffset = 10;
	 yOffset = 20;

     $('img').each(function(){
        //preloader
        if($(this).attr('hoversrc'))
        {
            $preload[$iterator] = $('<img src="'+$(this).attr('hoversrc')+'"/>');
            $iterator++;

            $(this).hover(
            //mouse over
            function(){
                $(this).attr({'default':$(this).attr('src'),'src':$(this).attr('hoversrc')});
            },
            //mouse out
            function(){
                $(this).attr({'src':$(this).attr('default')});
                $(this).removeAttr('default');
            });
        }

        //preloader
        if($(this).attr('popsrc'))
        {
            $preload[$iterator] = $('<img src="'+$(this).attr('popsrc')+'"/>');
            $iterator++;

            $(this).hover(
            //mouse over
            function(e){
                $("body").append("<div id='imagepop'><img src='"+$(this).attr('popsrc')+"'/></div>");
                $("#imagepop")
                    .css({"top":(e.pageY - xOffset) + "px","left":(e.pageX + yOffset) + "px",'border':'2px solid #000000','position':'absolute'})
                    .show();
            },
            //mouse out
            function(){
                $("#imagepop").remove();
            })

            $(this).mousemove(function(e){
                $("#imagepop").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px");
            });
        }
     });
});