$(document).ready(function(){
	if ($("#contact_form").length > 0) {
    	$("#contact_form").validate({
			errorClass: "error_msg",
			errorElement: "span",
			rules: {
				name: "required",
				email: {
					required: true,
					email: true
				},				
				title: {
					required: true
				},
				request: {
					required: true
				}
			}
		});		
	}
	if ($("#comment_form").length > 0) {
    	$("#comment_form").validate({
			errorClass: "error_msg",
			errorElement: "span",
			rules: {
				title: "required",
				description: {
					required: true
				}
			}
		});		
	}
	if ($("#login_form").length > 0) {
    	$("#login_form").validate({
			errorClass: "error_msg",
			errorElement: "span",
			rules: {
				username: {
					required: true,
					email: true
				},
				password: {
					required: true
				}
			}
		});		
	}
	if ($("#register_form").length > 0) {
    	$("#register_form").validate({
			errorClass: "error_msg",
			errorElement: "span",
			rules: {
				username: {
					required: true,
					email: true
				},
				password: {
					required: true
				},
				password2: {
					required: true,
					equalTo: "#register_password"
				}				
			}
		});		
	}	
	if ($("#change_password").length > 0) {
    	$("#change_password").validate({
			errorClass: "error_msg",
			errorElement: "span",
			rules: {
				old_password: {
					required: true
				},
				password: {
					required: true
				},
				password2: {
					required: true,
					equalTo: "#password"
				}				
			}
		});		
	}
	
	if ($("#keyword").length > 0) {
		$("#keyword").focus(function() {
			if ($(this).val() == $(this).attr('helpText')) {
				$(this).val('');				
			}
		});
		$("#keyword").blur(function() {
			if ($(this).val() == '') {
				$(this).val($(this).attr('helpText'));				
			}
		});		
	}	
	if ($("#my_account").length > 0) {
    	$("#my_account").validate({
			errorClass: "error_msg",
			errorElement: "span",
			rules: {
				postcode: {
					digits: true
				},
				phone: {
					digits: true					
				}
			}		
		});		
	}	
	if ($("#payment_form").length > 0) {
    	$("#payment_form").validate({
			errorClass: "error_msg",
			errorElement: "span",
			rules: {
				payment_type: {
					required: true
				},
				name: {
					required: true
				},	
				family: {
					required: true
				},	
				country: {
					required: true
				},	
				city: {
					required: true
				},
				postcode: {
					required: true,
					digits: true
				},																	
				address: {
					required: true					
				},
				phone: {
					required: true,
					digits: true
				}				
			}		
		});		
	}
	
	if ($('#slideholder').length > 0) {
		$('#slideholder').slideshow({width: 649, height: 298, pauseSeconds:5, fadeSpeed: 1, caption: false});
	}							
	
	$('#search_keyword').focus(function(){$('#search_keyword').val('')});
});

function changeLanguage(url) {
	$.ajax({
	  url: url,
	  success: function(data) {
		window.location.href=window.location.href;
	  }
	});	
}

function changePanel(panel, section) {
	$('.section'+section+' li a').removeClass('current_tab');
	$('#link'+panel).addClass('current_tab');
	$('.content'+section+' .tab_row').removeClass('default_row');	
	$('#tab'+panel).addClass('default_row');
	$('#tab'+panel).css('margin-top', 0);	
}

function hide_order_items(node) {
	$(node).parents('.order_row').find('.item_orders').hide();
	$(node).parents('.order_row').find('.item_orders').html('');	
	$(node).hide();
	$(node).parents('.order_row').find('.view_item_orders').show();
}

function get_order_items(url, node) {
	$.ajax({
	  url: url,
	  success: function(data) {
	    $(node).parents('.order_row').find('.item_orders').html(data);
		$(node).parents('.order_row').find('.item_orders').show();
		$(node).parents('.order_row').find('.hide_item_orders').show();		
		$(node).hide();
	  }
	});
}

function orderItem(url, node) {
	var orderButtonText = $(node).html();
	$(node).html('Добавяне...');
	if ($('.order_success').length > 0) {
		$('.order_success').remove();
	}
	$.ajax({
		url: url,
	  success: function(data) {
		if (data != 'fail') {
			$('.view_cart').html(data);
		}
		else {
			alert('There was an error updating your shopping cart. Please try again later.');
		}
	  }
	});	
	$(node).html(orderButtonText);
	if (sLanguage == '_en') {
		$(node).parents('div:first').append('<span class="order_success" style="display: none">You have successfully added your item to the shopping cart</span>');			
	}
	else {
		$(node).parents('div:first').append('<span class="order_success" style="display: none">Успешно добавихте продукта във вашата количка</span>');			
	}
	$('.order_success').fadeIn('slow');	
	setTimeout(function() {
		$('.order_success').fadeOut('slow');
	}, 3000);	
}
