var w,r,i,p,t,l,c,z,e,m,b,k,u,s,CPH,TOKEN;
var z = new Array();
var ROOT;

function Search(v) {
	v=v?v:$('#search input').val();
	window.location.href = "https://www.steps.org.au/Shop/Search.html?kw="+encodeURIComponent(v);
}

$(document).ready(function(){
	CPH = $('#copyright').height();
	TOKEN = $('#TOKEN').val();
	ROOT = $('#ROOT').val();
	
	$.ajaxSetup({
		url: ROOT+'~do',
		type: 'POST'
	});
	
	$('input,textarea').bind('focus blur', function(){
		var t=$(this),v=t.val(),r=t.attr('rel'); if(!t.hasClass('noP')) t.val(v==r?'':(v==''?r:v));
	});
	
	$(window).bind("resize load",function(){
		if($(this).width()<1001) $('html').addClass('sml');
		else $('html').removeClass('sml');
		var w=$(this).height();
		$('body').height(($('body').height()<w+1?w:'auto'));
		chkBtm();
		setLoader();
	});
	
	setTimeout("$('#hH1').fadeOut(500)",3000);
		
	$('.noJava').each(function(){
		$(this).replaceWith('<a class="'+$(this).attr('class').replace('noJava','')+' bu lge"'+($(this).attr('id') ? 'id="'+$(this).attr('id')+'"' : '')+' href="'+$(this).attr('rel')+'">'+$(this).val()+'</a>');
	});
	mkButtons();
	
		// Slider Initialization
	if ($('.slider').size()) {
		$('.slider').each(function(){
			l = '#'+$('#'+$(this).attr('id')+'CTRL').attr('id');
			s = $(l+' span').size();
			w = $(l+' div.bg').width();
			r = w / s;
			i = 1;
			z[l] = setTimeout("slide(" + s + ",'"+l+"')", 10000);
			$(l+' span').each(function () {
				t = $(this);
				if (i == 1) p = 31;
				else p = p + (w - 115) / (s - 1);
				t.css('left', Math.round(p) + 'px');
				if (t.hasClass('sel')) $(l+' .knob').stop().animate({
					left: parseInt(t.css('left').replace("px", "")) + 6
				}, 500);
				i++;
			}).click(function () {
				l = '#'+$(this).parent().parent().attr('id');
				s = $(l+' span').size();
				tme(s,l);
				t = $(this);
				if (!t.hasClass('sel')) {
					$(l+' span').removeClass('sel');
					t.addClass('sel');
					sMveA(t.css('left'), parseInt(t.html()),l);
				}
			});
		});
	}
	jsEnhance();
});

function jsEnhance(){
	$('a.js').each(function(){ $(this).attr('href',$(this).attr('rel')); });
}

function mkButtons(){
	$('a.bu').each(function(){var t=$(this);if(!t.children().size())t.addClass('j').html('<span class="a"></span><span class="b">'+t.html()+'</span><span class="c"></span>');t.css('margin-left',(-7+(t.parent().innerWidth()-t.width())/2)+'px');}).css({'background':'transparent'}).bind('mouseover mouseout', function(){ $(this).toggleClass('hov') }).bind('mousedown mouseup', function(){ $(this).toggleClass('hov actv') }); // Create buttons, align & add events
}

// Slider Functions
function sMve(d, l) {
	s = $(l+' span').size();
    tme(s,l);
    t = $(l + ' span.sel');
    c = parseInt(t.html());
    m = 0;
    if (d == 'l' && c > 1) {
        t = t.removeClass('sel').prev().addClass('sel');
        m = 1;
        c = c - 1;
    } else if (d == 'r' && c < $(l + ' span').size()) {
        t = t.removeClass('sel').next().addClass('sel');
        m = 1;
        c = c + 1;
    }
    if (m) sMveA(t.css('left'), c, l);
}

function sMveA(p, c, l) {
    $(l+' .knob').stop().animate({
        left: parseInt(p.replace("px", "")) + 6
    }, 200);
    $(l).parent().prev().find('.rotatorOuter').stop().animate({
        marginLeft: -(590 * (c - 1))
    }, 200);
}

function tme(s,l) {
    clearTimeout(z[l]);
    z[l] = setTimeout("slide(" + s + ",'"+l+"')", 20000);
}

function slide(s,l) { // Number of blocks, and ID of slider
    t = $(l+' span.sel');
	t.removeClass('sel');
    c = parseInt(t.html());
    c = c == s ? 1 : c + 1;
    t = c == 1 ? $(l+' span:first').addClass('sel') : t.next().addClass('sel');
    sMveA(t.css('left'), c, l);
    z[l] = setTimeout("slide(" + s + ",'"+l+"')", 10000); // 12000
}

function chkBtm(){
	var cp = $('#copyright');
	var cph = cp.offset().top+CPH;
	var wh = $(window).height();
	if(cph<wh) cp.height(wh-(cph-CPH)-11);
}

// Show/Hide and position the animating ajax loader
function setLoader(b,x,y){
	b = b ? (typeof x=="object" ? b : $(b)) : false;
	var l = $('#loader');
	if(!b)
		l.stop().fadeOut('fast');
	else
		l.css({
			'left':Math.ceil(b.offset().left - (b.outerWidth()/2 - l.outerWidth()/2) + 100 + (x?x:0))+'px',
			'top': Math.ceil(b.offset().top + (b.outerHeight()/2 - l.outerHeight()/2)+(y?y:0))+'px'
		}).stop().fadeIn(500);
}

function errors(){
	$(':input.error').unbind('focus').focus(function(){
		$(this).removeClass('error');
	});
}

function doErrors(e,d){
	e=e.split(',');
	for(i in e){
		$((d?d+' ':'')+'[name="'+e[i]+'"]:visible').addClass('error');
	}
	errors();
}
