//jQuery
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
		
$(document).ready(function(){

	//depending on what page the user is on, show the sub category menu
	var url = window.location.pathname;
	
	if(url=='/printer_select_result.php'){
		$('#ink').slideToggle();	
	} else if (url=='/catalogue.php'){
		//on the catalogue page, therefore we need to find out what category it is.
		var categoryid = '#prodcat'+getUrlVars()['selected_cat'];
		$(categoryid).slideToggle('slow');
	}
	
  	$("#brandselect").change(function(){

     $.ajax({
            type: 'POST',
			url: '/catalogue/includes/ajax/get-make.php',
			data: 'm='+$(this).val(),
			success: function(html){
                $('#modelselect').empty();
                $('#modelselect').html('<option value="-1">Select a Model</option>');
                $('#modelselect').append(html);
            }
     });
  });

  	$("#modelselect").change(function(){
		var brand_val = $("#brandselect").val();
		var model_val = $(this).val();
		
		window.location = 'printer_select_result.php?p_make='+brand_val+'&p_model_id='+model_val;  	
  	});
      
   $("#typeselect").change(function(){
		var type_val = $(this).val();
		
		window.location = 'printer_select_result.php?p_type='+type_val;  	
  	});
  	
  $('#login-button').click(function(){
    	var user = $('#username').val();
    	var pass = $('#password').val();
    	
		$('#myaccount div').hide();
		$('#myaccount .ajax-loader').fadeIn('fast');
		$.ajax({
	    		type: 'POST',
				url: '/catalogue/includes/ajax/login.php',
				data: 'u='+user+'&p='+pass,
				dataType: 'json',
				success: function(data){ 						
					if(data.result=='failed'){
						$('#dialog')
							.html('The username and password did not match. <br />Please try again')
							.dialog({
								autoOpen: false,
								title: 'Login failed',
								bgiframe: true,
								modal: true,
								draggable: false,
								resizable: false,
								buttons: {
									Ok: function() {
										$(this).dialog('close');
									}
								}
							});
						$("#dialog").dialog('open');							
					} else {
						$('#myaccount div').html(data.output);	
					}
				} // end ajax success function
			});
		
		$('#myaccount .ajax-loader').fadeOut('slow',function(){
			$('#myaccount div').fadeIn('slow');
		});
					               		
	});
	
	$('.accordion .navheading-left').click(function(){
		$(this).next().slideToggle();
	});
	
	$('#faqs').accordion({
			autoHeight: false,
			collapsible: true,
			icons: {
    			header: "ui-icon-info",
   				headerSelected: "ui-icon-info"
			}

	});

	$('.addcart-question a').click( function(){
		
		var heading = $(this).parent().next().children('h4').text();
		var content = $(this).parent().next().children('div').html();
		
		$('#dialog').empty();
		$('#dialog').dialog('option','title',heading);
		$('#dialog')
			.html(content)
			.dialog({
				autoOpen: false,
				title: heading,
				bgiframe: true,
				modal: false,
				draggable: false,
				resizable: false,
				buttons: {
					Ok: function() {
						$(this).dialog('close');
					}
				}
			});
		$("#dialog").dialog('open');		
	});
	
	function deleteCartItem(itemId,itemType){
		
		$('#cart').empty().html('<img src="/images/ajax-loader.gif" style="margin:0px auto" />');		
		$.ajax({
			type: 'POST',
			url: '/catalogue/includes/ajax/delete-from-cart.php',
			data: 'item='+itemId+'&type='+itemType,
			success: function(result){
				//refresh cart
				setTimeout(function(){
					$('#cart').empty().html(result);
					$('a.delete-cart-item').click( function(){		
						
						deleteCartItem( $(this).children('input').val(), $(this).children('input').attr('name') );						
					});	
				},1000);		
										
			}
		});
	}
	
	$('.product-list-button a').click( function(){
		var clickedCell = $(this);
		$(this).fadeOut(250, function() {
			
			$(this).parent().prepend('<img src="/images/ajax-loader.gif" style="margin:0px auto" />');
			$('#cart').empty().html('<img src="/images/ajax-loader.gif" style="margin:0px auto" />');
			$.ajax({
				type: 'POST',
				url: '/catalogue/includes/ajax/add_to_cart.php',
				data: 'item='+$(clickedCell).parent().children('input').val(),
				success: function(result){
					//refresh cart 				
					setTimeout(function(){
						$('#cart').empty().html(result);					                 
						$(clickedCell).parent().children('img').fadeOut(250,function(){
							$(clickedCell).fadeIn(250);	
						});
						$('a.delete-cart-item').click( function(){		
							
							deleteCartItem( $(this).children('input').val(), $(this).children('input').attr('name') );						
						});					
					},1000);
				}
			});
				
		});		
	});



	$('a.delete-cart-item').click( function(){
		
		deleteCartItem( $(this).children('input').val(), $(this).children('input').attr('name')  );		
	});


});

/*
 * jQuery Watermark plugin
 * Version 1.0 (14-SEP-2009)
 * @requires jQuery v1.2.3 or later
 *
 * Examples at: http://mario.ec/projects/jqwatermark/
 * Copyright (c) 2009 Mario Estrada
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/mit-license.php
 *
 */

(function($)
{
	$.fn.watermark = function(text, css_options){
		css = $.extend({
			color : '#999',
			left: 4
		}, css_options);
		
		return this.each(function()
		{
			var input_marked = $(this);
			var label_text = text === undefined ? $(this).attr('title') : title;
			var watermark_container = $('<span class="watermark_container" style="position: relative"></span>');
			var watermark_label = $('<span class="watermark">' + label_text + '</span>');
			
			watermark_container.css({
				float: input_marked.css('float')
			});
			
			$(this).wrap(watermark_container);
			
			var height = $(this).outerHeight();

			checkVal($(this).val(), watermark_label);
			
			watermark_label.css({
				position: 'absolute',
				fontSize: $(this).css('font-size'),
				color: '#999999',
				left: (parseInt($(this).css('padding-left')) + css.left) + 'px',
				top: '48%',
				height: height + 'px',
				lineHeight: height + 'px',
				marginTop: '-' + (height / 2) + 'px'
			});
			
			watermark_label.click(function()
			{
				$(this).next().focus();
			})

			$(this).before(watermark_label)
			.focus(function()
			{
				checkVal($(this).val(), watermark_label);
				watermark_label.animate({ opacity : 0.4}, 250);
				//watermark_label.fadeOut({endOpacity:0.40,duration:0.75});
			})
			.blur(function()
			{
				checkVal($(this).val(), watermark_label);
				watermark_label.animate({ opacity : 1}, 250);
				//watermark_label.fadeIn('slow');
			})
			.keydown(function(e)
			{
				$(watermark_label).hide();
			});
		});
	};
	
	checkVal = function(val, elem)
	{
		if(val == '') 
			$(elem).show();
		else 
			$(elem).hide();
	};
	
	$(document).ready(function()
	{
		$('input.jq_watermark[type=text], textarea.jq_watermark, input[type=password].jq_watermark,').each(function(){
			$(this).watermark();
		});
	});
})(jQuery);

// JavaScript
function populate(sel,modelSel) {
	selVal = sel.options[sel.options.selectedIndex].value;
	if (selVal) {
		modelSel.disabled = false;
		selLength = make[selVal].length
		modelSel.options.length = selLength
		for (i=0; i < selLength; i++) {
		  modelSel.options[i] = new Option(make[selVal][i],m_id[selVal][i]);
		  if (i==0) {
			 modelSel.options[i].selected=true;
		  }
		}
	} else { // no id
		modelSel.options.length = 1;
		modelSel.options[0] = new Option("- - - - - - - - - - - - - - - - -","");
		modelSel.disabled = true;
	}
}

function is_digit()
{
  	if ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode == 46)){
		return event.keyCode
		
	}else{
		return null;
	}
}

function info_pop(url){
	window.open(url,"info","toolbars=no,scrollbars=no,status=no,width=250,height=250,address=no,left=200,top=200");
}


