jQuery(document).ready(function() {
	
	var $ = jQuery;
	// VirtueMart Menu System

	$('#tabs1 div.moduletable li > ul').each(function() {
		// Find this list's parent list item.
		var parent_li = $(this).parent('li');
		if(!parent_li.hasClass('conflict')) {
			var sub_ul = $(this).remove();
			parent_li.wrapInner('<a/>').find('a').click(function() {
				// Make the anchor toggle the leaf display.
				sub_ul.toggle();
				return false;
			});
			parent_li.append(sub_ul);
		}
	});

	// Hide all lists except the outermost.
	$('#tabs1 div.moduletable ul ul').hide();
	$('#tabs1 div.moduletable ul li.active').each(function() {
		$(this).children('ul:first').show();
	});

	// Virtuemart Add To Favorites
	$('a.add_to_favorites').click(function() {
		var link = $(this);
		var span = link.children('span');
		origclass = span.attr('class');
		span.removeClass('add remove');
		span.addClass('loading');
		product_id = span.attr('id');
		product_id = product_id.split('.');
		product_id = product_id[1];

		$.ajax({
			url: 'favorites_ajax.php',
			type: "POST",
			dataType: 'json',
			data: 'product_id='+product_id,
			success: function(msg) {

				result = msg.result;
				pid = msg.product_id;
				span.removeClass('loading');
				if(result == 'added') {

					newclass = 'remove';
					title = 'Remove from Favorites';
				}
				else if(result == 'removed') {

					newclass = 'add';
					title = 'Add to Favorites';
				}
				else if(result == 'failed') {

					alert('Error: '+msg.error);
					span.addClass(origclass);
					return false;
				}
				span.addClass(newclass);
				link.attr('title',title);
				// Now display added/removed message
			},
			error: function(xhr,text,err) {

				alert('Error: '+xhr.status+'\r\n'+text+'\r\n'+err);
			}
		});
	});

	// Virtuemart Add to Cart
	$('form.addtocart_form input.addtocart_button').click(function() {

		var form = $(this).parents('form.addtocart_form');
		var product_name = form.parents('td:first').siblings('td.product_name').children('span:first').html();
		var product_image = form.parents('tr:first').find('td a[rel=example1]').attr('href');
		if(product_image != undefined && product_image != '') {

			product_image = product_image.substring(product_image.lastIndexOf('/')+1);
			product_image = encodeURI('components/com_virtuemart/show_image_in_imgtag.php?filename='+product_image+'&newxsize=48');
		}
		else {

			product_image = 'components/com_virtuemart/shop_image/ps_image/epay_images/button_ok.png';
		}

		url = form.attr('action');
		var data = form.serialize();
		$.ajax({
			url: url,
			type: "POST",
			data: data,
			success: function(msg) {

				popup_prod_added(product_name,product_image);
				update_Mini_Cart();
			},
			error: function(xhr,text,err) {

				alert('Error: '+xhr.status+'\r\n'+text+'\r\n'+err);
			}
		});
		return false;
	});

	$('#vmMainPage .add_favorites_to_cart').click(function() {

		product_image = 'components/com_virtuemart/shop_image/ps_image/epay_images/button_ok.png';

		var form_id = 'bulk_cart';
		if($('#'+form_id).length == 0) {

			$('form.addtocart_form:first').clone().appendTo('body').hide().attr('id',form_id).removeClass('addtocart_form');
		}

		var form = $('#'+form_id);
		form.children('input[name=prod_id\[\]],[name=quantity\[\]],[name=product_id]').remove();
		$('form.addtocart_form').each(function() {

			form.append('<input type="hidden" name="prod_id[]" value="'+$(this).children('input[name="prod_id\[\]"]').val()+'" />');
			form.append('<input type="hidden" name="quantity[]" value="'+$(this).children('input[name="quantity\[\]"]').val()+'" />');
		});

		var data = form.serializeArray();
		var url = form.attr('action');

		form.remove();

		$.ajax({
			url: url,
			type: "POST",
			data: data,
			success: function(msg) {

				popup_prod_added('Favourites Added to Cart',product_image);
				update_Mini_Cart();
			},
			error: function(xhr,text,err) {

				alert('Error: '+xhr.status+'\r\n'+text+'\r\n'+err);
			}
		});
		return false;
	});

	function update_Mini_Cart() {

		$.ajax({
			url: '/index2.php',
			type: 'POST',
			data: 'option=com_virtuemart&page=shop.basket_short&only_page=1',
			success: function(output) {
				$('.vmCartModule').html(output);
			},
			error: function(xhr,text,err) {

				alert('Error: '+xhr.status+'\r\n'+text+'\r\n'+err);
			}
		});
	}

	function popup_prod_added(text,image) {

		var gr_text = text;
		var gr_image = image;
		$.gritter.add({
			title: 'Adding to Cart',
			text: gr_text,
			image: gr_image,
			time: 1000
		});
	}

	$("#slides").slides({
		container: 'bannergroup',
		slideEasing: 'easeInOutExpo',
		generatePagination: false,
		play: 5000,
		slideSpeed: 1000
	});


	$('a[rel="example1"]').colorbox({rel:'nofollow'});

	function switch_tabs(obj) {

		$('.tab-content').hide();
		$('.tabs a').removeClass("selected");
		var id = obj.attr("rel");
		$('#'+id).show();
		obj.addClass("selected");
	}
	
	$('.tabs a').click(function(){
		
		switch_tabs($(this));
	});

	switch_tabs($('.defaulttab'));

});
