var Promos = {current: 1, num:1, shown:0};
var Globals = {shortTime: 10000, longTime: 10000};

function switchContent(el){

    $clear(timer);
    if(Promos.current != el){
        //Promos.current = el;
        changeContent(el);
        startTimer(Globals.longTime);
    }
}

function changeContent(el){
    //setup tween
    newEl = $("content"+el.toString());


    currentEl = $("content"+Promos.current);
    newEl.set('tween',{duration: 1000});
    newEl.fade(0.999);

    if(newEl != currentEl){
        currentEl.fade(0);
    }

    Promos.current = el;

}

function page(dir){
    $clear(timer);
    next = (Promos.current + dir)%Promos.num;

    if(next == 0)
        next = Promos.num;

    changeContent(next);

    startTimer(Globals.shortTime);
}

var addCount = function(){this.shown++; $clear(timer); changeContent((this.current%this.num)+1); startTimer(Globals.shortTime);};


/* amended by Avalade per Erik's suggestion */

function selectRandom(){
    //var j = Math.floor(((Math.random()*10)%Promos.num)+1);

    var j = $random(1, 100);

    var randomPromo = 0;

    switch(true){
        case ((0 < j) && (j<50)):

            randomPromo = 1;

            break;

        case ((49 < j) && (j<100)):

            randomPromo = 2;

            break;

        /*
        case ((80 < j) && (j<91)):

            randomPromo = 3;

            break;

        case ((90 < j) && (j<101)):

            randomPromo = 4;

            break;
        */
    }

    changeContent(randomPromo);

    $('loader').fade(0);
}

/* end of amendment */

function startTimer(time){
    /*
    if(Promos.shown < Promos.num)
        timer = addCount.periodical(time, Promos); //Will add the number of seconds at the Site.
    else
        $clear(timer);
    */
    timer = null;
}

function initFade(){
    for(i=1;i<=Promos.num;i++){
        newEl = $("content"+i);
        newEl.fade(0);
    }
}

function showStyles(obj){
    alert($(obj).getStyle('margin'));
}

function initControls(){
    var controlDivs = $('container_wrapper').getElements('div.controlLinks');

    for (var i in controlDivs)
    {
        controlDivs[i].innerHTML = $('controls').innerHTML;
    }

}

function showSpecsCallout(){
    $('specsCallout').fade(1);
}

function hideSpecsCallout(){
    $('specsCallout').fade(0);
}

var compareBox_state = 'closed';
function openCompareBox(){

    if(compareBox_state == 'closed'){
        $('compareBox').set('tween',{duration: 1500,transition: Fx.Transitions.linear.easeOut});
        $('compareBox').tween('height', 200);

        compareBox_state = 'open';
    }
    else{
        $('compareBox').set('tween',{duration: 1500,transition: Fx.Transitions.linear.easeOut});
        $('compareBox').tween('height', 0);
        compareBox_state = 'closed';
    }

}



function initCompareBox(){
    //IF - Check for 'navtop_desc' object
    if($('compareBox')){
        $(document.body).getElements('a.compareButton').addEvent('click', function(){
                openCompareBox();
        });
        $(document.body).getElements('a.compareButton').setProperties({
            href: '#compare'
        });

    }


}

function initSpecsEvent(){
    $(document.body).getElements('a.specs').addEvents({
        'mouseenter': function(){
            showSpecsCallout();
        },
        'mouseleave': function(){
            hideSpecsCallout();
        }
    });
}

function stop(){
    $clear(timer);
}

function SearchBarFunction(){
	$$('#gnav li.search input[id=q]').each(function(search){
		var def = search.defaultValue;
		search.addEvent('focus',function(ev){
			search.value = (search.value.length==0||search.value==def)?'':search.value;
			search.defaultValue = '';
		}).addEvent('blur',function(ev){
			search.value = search.value.length==0?'':search.value;
		});
	});
}



window.addEvent('domready', function(){
	SearchBarFunction();

    initNavTopDesc();

    initControls();
    initCompareBox();
    initFade();
    //initSpecsEvent()

    // Disable random slideshow
    //setTimeout(selectRandom, 200);

    startTimer(Globals.shortTime);

    // Show first slide
    changeContent( 1 );
    window.setTimeout( function() { $('loader').fade(0); }, 1000 );
});