$(document).ready(function(){
	// initialise plugins
	jQuery('ul.sf-menu').superfish();
	jQuery("#SliderSingle").slider({
		from: 0,
		to: 200,
		step: 1,
		round: 0,
		dimension: '&nbsp;$',
		skin: "round",
		callback: function( val ) {
			document.narrow_search.page.value = 1;
			document.narrow_search.price.value = val;
			document.narrow_search.submit();
		}
	});
	$( 'div.catgry ul li' ).click( function() {
		window.location.href = $(this).find( 'a' ).attr( 'href' );
	} );
	if( typeof $('a[rel=lightbox]').lightBox != 'undefined' ) {
		$('a[rel=lightbox]').lightBox( {
			imageBtnClose: base_url+'/images/lightbox-btn-close.gif',
			imageLoading: base_url+'/images/lightbox-ico-loading.gif',
			imageBtnPrev: base_url+'/images/lightbox-btn-prev.gif',
			imageBtnNext: base_url+'/images/lightbox-btn-next.gif'
		} );
	}
	if( typeof $.fn.datepicker != 'undefined' )
		$("#date").datepicker();
	$('#brand_select').change( function() {
		url = $(this).val();
		if( url.match( /^http:/ ) )
			window.location.href = url;
		else
			window.location.href = base_url+url;
	} );
	$('#search_string').keyup( function( e ) {
		$('#'+this.id+'_ss').load( base_url+'_smart_search.php?name='+escape( this.value ) );
		if( e.keyCode != 13 ) return;
		post_search( $(this).val() );
	} );
	$('#searchbtn').click( function( e ) {
		var val = $('#search_string').val();
		if( val == 'Search Products' )
			val = '';
		post_search( val );
	} );
	if( $('#login_error').text() != '' )
		$('#login_error').show();
	$('div.address').hover(
		function() {
			$(this).addClass( 'address_hover' );
		}, function() {
			$(this).removeClass( 'address_hover' );
		}
	);
	$('input[name=payment_option]').click( function() {
		if( $(this).val() == 'cc' ) {
			$('#ccinfohdr').show();
			$('#ccinfo').show();
		} else {
			$('#ccinfohdr').hide();
			$('#ccinfo').hide();
		}
	} );
	hide_popup( 'please_wait_div' );
});

function show_pic( url, container ) {
	$('#ajax_window').remove();
	var list = $('<div id="ajax_window">Updating...</div>').appendTo("body");
	list.css('position', 'absolute');
	list.css('border', '1px solid #ddd');
	list.css('padding', '5px');
	list.css('background-color', '#f6f6f6');
	list.css('z-index', '50');
	list.css('left', $('#'+container).offset().left);
	list.css('top', $('#'+container).offset().top+20);
	
	list.html( '<a href="#" onclick="window.location.href = $(\'#'+container+'\').attr( \'href\' )"><img src="'+url+'" alt="" width="80" /></a>' );
	
}

function post_search( val ) {
	show_popup( 'please_wait_div' );
	window.location.href = base_url+'search-result/'+escape( val );
}

function post_login( frm ) {
	$('#login_error').html( "<img src='"+base_url+"images/loading-small.gif' width='16' height='16' alt='Loading...' align='absmiddle' /> Loading, please wait." ).show();
	$.post( base_url+'_login.php', { email: frm.email.value, password: $.sha1( frm.password.value ) }, handle_login );
}

function handle_login( data ) {
	if( data.match( /^Error/ ) )
		$('#login_error').text( data ).show();
	else {
		var date = new Date();
		if( $('#remember')[0].checked ) {
			date.setTime(date.getTime()+(3*365*86400*1000));
			document.cookie = 'pnremember='+$('#login_email').val()+'; expires='+date.toGMTString()+'; path=/'
		} else {
			date.setTime(date.getTime()-86400*1000);
			document.cookie = 'pnremember=; expires='+date.toGMTString()+'; path=/'
		}
		$('#login1').replaceWith( data );
		$('#login_error').hide();
		$('#login1').show();
		if( window.location.href.match( /checkout.php$/ ) || ( typeof TabbedPanels1 != 'undefined' ) )
			window.location.reload();
	}
}

function edit_address( id ) {
	$('#address'+id).html( "<img src='"+secure_base_url+"images/loading-small.gif' width='20' height='20' alt='Loading...' align='absmiddle' /> Loading, please wait." ).show();
	$.get( secure_base_url+'_edit_address.php', { id: id }, function( data ) {
		$('#address'+id).replaceWith( data );
	} );
}

function new_address( type ) {
	var div = $('<div id="address_new"></div>').prependTo('#content2');
	div.addClass( 'checkout_address' );
	div.html( "<img src='"+secure_base_url+"images/loading-small.gif' width='20' height='20' alt='Loading...' align='absmiddle' /> Loading, please wait." ).show();
	$.get( secure_base_url+'_edit_address.php', { type: type }, function( data ) {
		div.replaceWith( data );
	} );
}

function expand_login() {
	$("#login1").slideToggle("slow");
}

function show_popup( el ) {
	showopacity();
	//opera Netscape 6 Netscape 4x Mozilla 
	if (window.innerWidth || window.innerHeight){ 
		docwidth = window.innerWidth; 
	} else 
	//IE Mozilla 
	if (document.body.clientWidth || document.body.clientHeight){ 
		docwidth = document.body.clientWidth; 
	} else
		docwidth = 640;
	t = document.body.parentNode.scrollTop + 220;
	l = document.body.parentNode.scrollLeft + docwidth / 2 - 50;
	$('#'+el).css( 'top', t+'px' );
	$('#'+el).css( 'left', l+'px' );
	$('#'+el).show();
	showopacity();
}

function hide_popup( el ) {
	$('#'+el).hide();
	hideopacity();
}

function showopacity() {
	$('#screen_opacity').height( document.body.parentNode.scrollHeight + 'px' );
	$('#screen_opacity').width( document.body.parentNode.scrollWidth + 'px' );
	$('#screen_opacity').show();
}

function hideopacity() {
	$('#screen_opacity').hide();
}

function brand_tab( id ) {
	//$('div.brand').hide();
	$('div.tabs ul li a').removeClass( 'active' );
	$('div.tabs ul li.'+id+' a').addClass( 'active' );
	//$('#'+id).show();
	$('div.product_items').hide();
	$('#product_items_'+id).show();
}

function product_tab( id ) {
	$('div.product_tab').hide();
	$('#'+id).show();
}

function check_product_selection() {
	if( $('input:checked[name^=products]').length == 0 ) {
		alert( "Please select at least one product to add to your cart." );
		return false;
	}
	return true;
}

function update_qty( inp ) {
	m = inp.name.match( /^update_(\d+)$/ )
	id = m[1];
	$('#loader_'+id).show();
	inp.form.submit();
	inp.form.checkoutbtn.disabled = true;
	inp.form.continuebtn.disabled = true;
}

var ship_country = '';
var ship_zip = '';
function update_cart( country, standard_method, zip, atype ) {
	// check address type and same shipping checkbox/hidden
	if( atype == 'bill' ) {
		// same shipping must be on
		el = document.checkout_form.same_billing;
		if( el.type == 'checkbox' && !el.checked )
			return;
		if( el.type == 'hidden' && el.value != 1 )
			return;
	}
	method = ( standard_method ? 'Standard Delivery' : 'Express Delivery' );
	params = { 'shipping_method' : method };
	if( country ) {
		ship_country = country;
		params['ship_country'] = country;
	} else
		params['ship_country'] = ship_country;
	if( zip ) {
		ship_zip = zip;
		params['ship_zip'] = zip;
	} else
		params['ship_zip'] = ship_zip;
	$.post( base_url+'_leftnav_cart.php', params, function(data) {
		$('#leftnav_cart').replaceWith(data);
	});
}
