$(document).ready(function() {
	// Tabs
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	$("ul.tabs li").click(function() {

		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});

	
	// Toggle
	$(".toggle_container").hide(); 

	$(".toggle_trigger").click(function(){
		$(this).toggleClass("toggle_active").next().slideToggle("fast");
		return false;
	});
	// Contact Form

	$('#send_message').click(function(e){



		e.preventDefault();

		var error = false;

		var name = $('#name').val();

		var email = $('#email').val();

		var subject = $('#subject').val();

		var message = $('#message').val();



		if(name.length == 0){

			error = true;

			$('#name_error').fadeIn(500);

		}else{

			$('#name_error').fadeOut(500);

		}

		if(email.length == 0 || email.indexOf('@') == '-1'){

			error = true;

			$('#email_error').fadeIn(500);

		}else{

			$('#email_error').fadeOut(500);

		}

		if(message.length == 0){

			error = true;

			$('#message_error').fadeIn(500);

		}else{

			$('#message_error').fadeOut(500);

		}



		if(error == false){

			$('#send_message').attr({'disabled' : 'true', 'value' : 'Sending...' });

			var contactformurl = mysiteurl+"/js/send_email.php";



			$.post(contactformurl, $("#contact_form").serialize(),function(result){

			if(result == 'sent'){

			$('#cf_submit_p').remove();

			$('#mail_success').fadeIn(500);

			}else{

			$('#mail_fail').fadeIn(500);

			$('#send_message').removeAttr('disabled').attr('value', 'Email Us');

			}

			});

		}

	});



	
	//	Complaint Form
	$(function() {

		$("#visit_date").attr( 'readOnly' , 'true' ).datepicker({
			maxDate: 0, dateFormat : 'dd M yy'
		});

	});

	$('#feedback_category').change(function() {
	  	if ($('#feedback_category').val() == 'Complaints') {
	  		$('p.hide-field').show();
	  	} else {
	  		$('p.hide-field').hide();
	  	}
	});

	$('#send_complaint').click(function(e){
		e.preventDefault();
		var error = false;
		var name = $('#name').val();
		var contact = $('#contact').val();

		var email = $('#email').val();
		var feedback_category = $('#feedback_category').val();

		var visit_store = $('#visit_store').val();
		var visit_date = $('#visit_date').val();

		var visit_time = $('#visit_time').val();

		var complain_receipt = $('#complain_receipt').val();

		var comment = $('#comment').val();
		// Validate Name
		if(name.length == 0){
			error = true;
			$('#name_error').fadeIn(500);
		}else{
			$('#name_error').fadeOut(500);
		}				// Validate Contact
		if(contact.length == 0){

			error = true;

			$('#contact_error').fadeIn(500);

		}else{

			$('#contact_error').fadeOut(500);

		}
		
		// Validate Email
		if(email.length == 0 || email.indexOf('@') == '-1'){
			error = true;
			$('#email_error').fadeIn(500);
		}else{
			$('#email_error').fadeOut(500);
		}
		// Validate Feedback Category
		if(feedback_category.length == 0){

			error = true;

			$('#feedback_category_error').fadeIn(500);

		}else{

			$('#feedback_category_error').fadeOut(500);

		}

		// Validate Feedback Area
		if(($('#feedback_area_1').prop("checked") == false) && ($('#feedback_area_2').prop("checked") == false) && ($('#feedback_area_3').prop("checked") == false)){

			error = true;

			$('#feedback_area_error').fadeIn(500);

		}else{

			$('#feedback_area_error').fadeOut(500);

		}

		// Validate Visit Store
		if(visit_store.length == 0){

			error = true;

			$('#visit_store_error').fadeIn(500);

		}else{

			$('#visit_store_error').fadeOut(500);

		}

		// Validate Visit Date
		if(visit_date.length == 0){

			error = true;

			$('#visit_date_error').fadeIn(500);

		}else{

			$('#visit_date_error').fadeOut(500);

		}
		
		// Validate Visit Time
		if(visit_time.length == 0){

			error = true;

			$('#visit_time_error').fadeIn(500);

		}else{

			$('#visit_time_error').fadeOut(500);

		}
		
		// Validate Receipt Upload
		if(complain_receipt.length != 0){
			
			var complain_receipt_ext = complain_receipt.split('.').pop().toLowerCase();
			
			if ((complain_receipt_ext == 'jpg') || (complain_receipt_ext == 'jpeg')) {
			
				$('#complain_receipt_error').fadeOut(500);
			
			} else {
				
				error = true;

				$('#complain_receipt_error').fadeIn(500);				
				
			}

		}else{

			$('#complain_receipt_error').fadeOut(500);

		}
		
		// Validate Comment
		if(comment.length == 0){
			error = true;
			$('#comment_error').fadeIn(500);
		}else{
			$('#comment_error').fadeOut(500);
		}

		if(error == false){						$('#send_complaint').attr({'disabled' : 'true', 'value' : 'Sending...' });							$('#contact_form').submit();						/*
			$('#send_complaint').attr({'disabled' : 'true', 'value' : 'Sending...' });			
			var contactformurl = mysiteurl+"/js/send_complaint.php";
			$.post(contactformurl, $("#contact_form").serialize(),function(result){
			if(result == 'sent'){
			$('#cf_submit_p').remove();
			$('#mail_success').fadeIn(500);
			}else{
			$('#mail_fail').fadeIn(500);
			$('#send_complaint').removeAttr('disabled').attr('value', 'Email Us');
			}
			});
*/			
		}
	});

	// Image Effect
	$('img.image_effect').hover(function() {
		$(this).stop().animate({"opacity": 0.6}, 300);
	}, function () {
		$(this).stop().animate({"opacity": 1}, 300);
	});
	
	$('.flickr_badge_image').hover(function() {
		$(this).stop().animate({"opacity": 0.6}, 300);
	}, function () {
		$(this).stop().animate({"opacity": 1}, 300);
	});
	
	// Reverse Image Effect
	$('img.image_reverse').css({"opacity": 0.6})
	$('img.image_reverse').hover(function() {
		$(this).stop().animate({"opacity": 1}, 300);
	}, function () {
		$(this).stop().animate({"opacity": 0.6}, 300);
	});	
	
	// Vertical Menu IE fix
	$(".sidebarmenu li").hover(function() {
		$(this).addClass("iehover");
	}, function() {
		$(this).removeClass("iehover");
	});
	
	// Removed for Snowflake theme
	// Homepage Accordion
/*	$( "#accordion" ).accordion({
		autoHeight: false,
		navigation: true,
		active: 0
	});*/
	// For iframe upload
	$('#iframeUpload').load(function() {
        var result = $('#iframeUpload').contents().find('#send_output').text();
        if (result == 'sent') {
			$('#cf_submit_p').remove();
			$('#mail_success').fadeIn(500);
			$('#mail_fail_upload').fadeOut(500);
			$('#mail_fail').fadeOut(500);
		} else if (result == 'upload_failed') {
			$('#mail_fail_upload').fadeIn(500);
			$('#send_complaint').removeAttr('disabled').attr('value', 'Email Us');
        } else {
			$('#mail_fail').fadeIn(500);
			$('#send_complaint').removeAttr('disabled').attr('value', 'Email Us');
        }
    });

});


