if (typeof console == 'undefined') {
	console = function(){}
	console.log = function(){}
} else console.log('Active Console');

$.fn.rollover = function(id) {
	$(this).data('cssWidth',$(this).css('width')).data('cssHeight',$(this).css('height')).css({width:'auto',height:'auto'});
	$(this).data('trueWidth',$(this).width()).data('trueHeight',$(this).height()).css({width:$(this).data('cssWidth'),height:$(this).data('cssHeight')});
	$(this).mouseenter(function(e){
		if (!$('#'+id).length) {
			$(this).after('<div id='+id+'>');
			$(this).data('removeOnEnd',true);
		} else $(this).data('removeOnEnd',false);
		$('#'+id).css({'background-image':'url('+$(this).attr('src')+')'}).show();
		$(this).bind('mousemove.preview',function(e){
			var thisTopRatio = (e.pageY-$(this).offset().top)/$(this).height();
			var thisLeftRatio = (e.pageX-$(this).offset().left)/$(this).width();
			var top = -1 * ((thisTopRatio*$(this).data('trueHeight'))-(thisTopRatio*$('#'+id).height()));
			var left = -1 * ((thisLeftRatio*$(this).data('trueWidth'))-(thisLeftRatio*$('#'+id).width()));
			$('#'+id).css({'background-position':left+'px '+top+'px'});
		});
	}).mouseleave(function(e){
		$(this).unbind('mousemove.preview');
		if ($(this).data('removeOnEnd')) $('#'+id).remove();
		else $('#'+id).hide();
	}).css({cursor:'pointer'});
}

$.fn.nav_link = function() {
	if ($(this).is('[query]')) {
		$(this).data('html',false);
		limit = parseInt($(this).attr('href').replace(/[^\d]/g,''))-1;
		query = $(this).attr('query')+' LIMIT '+limit+',10';
		$.post('/_php/products.php',{q:query,type:$(this).attr('rel')},function(data){
			var type = data.substr(0,4);
			data = data.substr(4);
			$('a[rel='+type+']').data('html',data);
		});
		/*$(this).click(function(e){
			if ($(this).data('html')!==false) {
				e.preventDefault();
				$(this).data('html',false);
				$('#products').html($(this).data('html'));
			}
		});*/
	}
}

$.fn.resetForm = function() {
	if ($(this).is('form')) {
		$(this)[0].reset();
	}
	return $(this);
}

$.fn.stepcarouselNav = function(galleryid) {
	$(this).css({'cursor':'pointer'}).data('galleryid',galleryid);
	if ($(this).hasClass('left')||$(this).hasClass('right'))
		$(this).css({'background-image':'none'});
	$(this).mouseenter(function(e){
		if ($(this).hasClass('left'))
			$(this).css({'background-image':'url(\'/_img/step_left.gif\')'});
		else if ($(this).hasClass('right'))
			$(this).css({'background-image':'url(\'/_img/step_right.gif\')'});
	}).mouseleave(function(e){
		if ($(this).hasClass('left')||$(this).hasClass('right'))
			$(this).css({'background-image':'none'});
	}).click(function(e){
		e.preventDefault();
		var step = 0;
		if ($(this).hasClass('left'))
			step = -1;
		else if ($(this).hasClass('right'))
			step = 1;
		else {
			stepcarousel.stepTo($(this).data('galleryid'),$(this).attr('move'));
			return true;
		}
		stepcarousel.stepBy($(this).data('galleryid'),step);
		return true;
	});
}

$.fn.descript = function(message) {
	$(this).each(function(){
		$(this).bind('mouseenter.descript',function(e){
			$('#descript_box').remove();
			$('body').append('<div id="descript_box">'+message+'</div>');
			$('#descript_box').css({
				position:'absolute',
				top		:($(this).offset().top+$(this).outerHeight())+'px',//e.pageY+'px',
				left	:$(this).offset().left+'px'
			});
		});
		$(this).bind('mouseleave.descript',function(e){
			$('#descript_box').remove();
		}).css({cursor:'pointer'});
	});
	return $(this);
}

anylinkcssmenu.init("anchorclass");

$(function(){
	$('[rel=external]').attr('target','_blank');
	
	//NAV MENU AND ANYLINK FIX
	$('.anylinkcss').each(function(i){
		blockWidth = $('#nav').find('a[rel='+$(this).attr('id')+']').outerWidth();
		$(this).find('.spacer .block').css({width:blockWidth+'px'});
		$(this).mouseenter(function(){
			rel = $(this).attr('id');
			$('.anchorclass[rel='+rel+']').addClass('hover');
		}).mouseleave(function(){
			rel = $(this).attr('id');
			$('.anchorclass[rel='+rel+']').removeClass('hover');
		});
	});
	
	//FORMS
	$('a.submit').live('click',function(e){
		e.preventDefault();
		$(this).parents('form:first').submit();
	});
	$('a.reset').live('click',function(e){
		e.preventDefault();
		$(this).parents('form:first').resetForm();
	});
	$('input[placeholder]').each(function(){
		$(this).data('cssColour',$(this).css('color'));
		if ($(this).val()=='')
			$(this).val($(this).attr('placeholder')).css({'color':'#999'});
		$(this).focus(function(){
			if ($(this).val()==$(this).attr('placeholder'))
				$(this).val('').css({'color':$(this).data('cssColour')});
		}).blur(function(){
			if ($(this).val()=='')
				$(this).val($(this).attr('placeholder')).css({'color':'#999'});
		});
		$(this).parents('form:first').submit(function(){
			$(this).find('input[placeholder]').each(function(){
				if ($(this).val()==$(this).attr('placeholder'))
					$(this).val('');
			});
		});
	});
	$('select option[parent]').each(function(){
		var ary = $(this).attr('parent').split(':');
		$(this).data('parentInput',$(':input[name='+ary[0]+']'));
		$(this).data('parentValue',ary[1]);
		if ($(this).data('parentInput').val()!=$(this).data('parentValue'))
			$(this).attr('disabled',true).hide();
		$(this).data('parentInput').unbind('change.parent').bind('change.parent',function(){
			var inputChanged = $(this);
			$('select option[parent]').each(function(){
				if ($(this).data('parentInput').attr('name')==inputChanged.attr('name')) {
					if ($(this).data('parentInput').val()==$(this).data('parentValue'))
						$(this).attr('disabled',false).show();
					else
						$(this).attr('disabled',true).hide();						
				}
			});
		});
	});
	$('select option[value=new]').parent().change(function(e){
		if ($(this).val()=='new') {
			var javaxid = $(this).attr('id');
			$.fancybox({
				autoScale:false,
				autoDimensions:false,
				width:'290px',
				height:'auto',
				href:'/_inc/address.php',
				ajax:{
					type:'post',
					data:{'javaxid':javaxid,'userid':userID}
				}
			});
		}
	});
	
	//PRODUCT LIST
	$('a[rel=next]').add('a[rel=prev]').nav_link();
	
	//IMAGE rollover
	/*$('img.rollover').load(function(){
		$(this).after('<div id="preview">');
		$('#preview').hide().css({height:$('img.rollover').height()+'px'});
		$(this).rollover('preview');
	});*/
	
	//FANCY BOX
	$('a[rel^=gallery]').fancybox({
		autoScale:false
	});
	
	//SEARCH FORM
	$('#search_form').find('.type_div').hide();
	if ($('#search_form').find('select[name=product_type]').val()!='')
		$('#search_form').find('#'+$('#search_form').find('select[name=product_type]').val()).show();
	$('#search_form').find('select[name=product_type]').change(function(){
		var type = $(this).val();
		$('#search_form').find('.type_div').hide();
		$('#search_form').find('#'+type).show();
	});
	
	var child = $('#search_form').find('select[name=tag[]] option[parent]:selected').attr('parent');
	$('#search_form').find('select[name=tag[]] option').each(function(){
		if ($(this).is('[parent_id]') && $(this).attr('parent_id')==child)
			$(this).attr('selected',true);
	});
	var parent = $('#search_form').find('select[name=tag[]] option[parent_id]:selected').attr('parent_id');
	$('#search_form').find('select[name=tag[]] option').each(function(){
		if ($(this).is('[parent]') && $(this).attr('parent')!=parent)
			$(this).attr('disabled',true).hide();
	});
	$('#search_form').find('select[name=tag[]]').change(function(){
		if ($(this).find('option:selected').is('[parent_id]')) {
			var parent = $(this).find('option:selected').attr('parent_id');
			$('#search_form').find('select[name=tag[]] option').each(function(){
				if ($(this).attr('parent')==parent)
					$(this).attr('disabled',false).show();
				else if ($(this).is('[parent]') && $(this).attr('value')!='') {
					if ($(this).is(':selected')) $(this).attr('selected',false);
					$(this).attr('disabled',true).hide();
				}
			});
		}
	});
	
	var child = $('#search_form').find('select[name=Projector Model] option:selected').attr('manufacturer');
	$('#search_form').find('select[name=Manufacturer] option').each(function(){
		if ($(this).val()==child && child!='')
			$(this).attr('selected',true);
	});
	var parent = $('#search_form').find('select[name=Manufacturer]').val();
	$('#search_form').find('select[name=Projector Model] option').each(function(){
		if ($(this).is('[manufacturer]') && $(this).attr('manufacturer')!=parent)
			$(this).attr('disabled',true).hide();
	});
	$('#search_form').find('select[name=Manufacturer]').change(function(){
		var parent = $(this).val();
		$('#search_form').find('select[name=Projector Model] option').each(function(){
			if ($(this).attr('manufacturer')==parent) {
				$(this).attr('disabled',false).show();
			} else if ($(this).attr('value')!='') {
				if ($(this).is(':selected')) $(this).attr('selected',false);
				$(this).attr('disabled',true).hide();
			}
		});
	});
	
	$('#search_box a.search').click(function(e){
		e.preventDefault();
		$(this).parents('form:first').submit();
	});
	
	$('#search_form #lamps select[name=lamps[Make]]').change(function(){
		$.post(
			'/_inc/lamp_model.php',
			{make:$(this).find('option:selected').val()},
			function(data){
				$('#search_form #lamps select[name=lamps[Model]]').html(data);
			}
		);
	});
	
	//PRODUCT PAGE
	$('h2.tab a').click(function(e){
		e.preventDefault();
		var item = $(this).attr("rel");
		$('.tabcontent:visible').animate({height: 'hide'},{ "duration": "fast"})
		$('.'+item).animate({height: 'show'},{ "duration": "slow"});
		$('h2.tab a').addClass('off');
		$(this).removeClass('off');
	});
	
	//BASKET PAGE
	$('a[href^=?delete]').click(function(e){
		e.preventDefault();
		$(this).parents('tr:first').find('input[name^=product_quantity]').val('0');
		$(this).parents('form:first').submit();
	});
	
	$('p.readonly').descript('Avaliable Soon');
});

//PRODUCT PAGE POPUP
function remove_product(prodid){
	var $order = $('form#order');
	IDinput = $order.find('input[name=product_id[]][value='+prodid+']').index();
	console.log(IDinput);
	if (IDinput!==-1) {
		$order.find('input[name=product_id[]]:eq('+(IDinput-1)+')').remove();
		$order.find('input[name=product_price[]]:eq('+(IDinput-1)+')').remove();
		return remove_product(prodid);
	} else return true;
}
function click_add(e) {
	//e.preventDefault();
	var $order = $('form#order');
	obj = e.target;
	var prodid = $(obj).attr('id').replace(/^product/,'');
	if ($(obj).hasClass('selected')) {
		remove_product(prodid);
		$(this).removeClass('selected');
	} else {
		var prodprice = $(obj).attr('price');
		$order.find('input[name=product_id]').attr('name','product_id[]');
		$order.find('input[name=product_price]').attr('name','product_price[]');
		$order.find('input[name=product_id[]]:last').after($order.find('input[name=product_id[]]:last').clone());
		$order.find('input[name=product_id[]]:last').val(prodid);
		$order.find('input[name=product_price[]]:last').after($order.find('input[name=product_price[]]:last').clone());
		$order.find('input[name=product_price[]]:last').val(prodprice);
		$(obj).addClass('selected');
	}
	return false;
}
function click_no(e) {
	var $order = $('form#order');
	//e.preventDefault();
	$.fancybox.close();
	$order.submit();
	return false;
}
