$(document).ready(function(){

	function onResize() {
		windowWidth = $(window).width();
		if (windowWidth <= 480) {
		  	isIpod = true;
		  	 		
		}
		else {
			isIpod = false;
		}
		$('.quote').removeAttr("style");

	}
	
	var windowWidth = $(window).width();
	var isIpod = false;
	if (windowWidth <= 480) {
	  isIpod = true;
	}
	
	$(window).resize(function(){
	  onResize();
	});

	if (!isIpod) {
		$("#lisabot").sprite({fps: 8, no_of_frames: 13});
	}

	$('#to_top').click(function(event){
		//prevent the default action for the click event
		event.preventDefault();

		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:0}, 300);
	});

	// Expand Panel
	$("#open").click(function(){
		$("#top_panel").animate({top: '0px'});	
	});	
	
	// Collapse Panel
	$("#close").click(function(){
		 $("#top_panel").animate({top: '-275px'});
	});		
	
	// Switch buttons on click
	$("#open_contact_form a").click(function () {
		$("#open_contact_form a").toggle();
	});
	
	
		
	$("#interactive").sprite({
    	fps: 6, 
        no_of_frames: 2,
        rewind: false,
        on_last_frame: function(obj) {
            obj.spStop();
        }
     });
	setInterval ( "$('#interactive').spStart()", 4000 );
 
 
 	var defaultQuote = "Glorm is a Creative Laboratory for hire!";
 	var quoteDelay = 1000;
 
 	$("#interactive").bind("click mouseenter", function() {
 	
 		if (!isIpod) {
	 		$("#interactive_animate").addClass("to_front");
	 		$('#interactive_animate').destroy();
	 		$("#interactive_animate").sprite({
			    fps: 24, 
			    no_of_frames: 34,
			    on_first_frame: function(obj) {
			    	obj.spStart();
			    }
	
			});
		}
		$(".quote").css("opacity",0).text("We make online and mobile Games and Apps.").animate({opacity:1},quoteDelay);
 	
 	});
 	
 	$("#interactive").bind("mouseleave", function() {
 		$(".quote").css("opacity",0).text(defaultQuote).animate({opacity:1}, quoteDelay);
 		if (!isIpod) {
	 		$('#interactive_animate').destroy();
	 		$("#interactive_animate").sprite({
			    fps: 24, 
			    no_of_frames: 10, 
			    on_last_frame: function(obj) {
	        		obj.spStop(); // stop the animation on the last frame
	    		}
			});
			
			setTimeout(function() {
	    		$("#interactive_animate").removeClass();
			}, 500);	
		} 
 	});
  	$("#motion").bind("click mouseenter", function() {

		if (!isIpod) {
	 		$("#motion_animate").addClass("to_front");
	 		$('#motion_animate').destroy();
	 		$("#motion_animate").sprite({
			    fps: 30, 
			    no_of_frames: 30, 
			    on_first_frame: function(obj) {
			        obj.spStart(); 
			    }, 
			    on_last_frame: function(obj) {
	        		obj.spStop(); // stop the animation on the last frame
	    		}	
			});
		}
		else {
			$("#interactive").animate({top: '20px'}, 400);
			$(".quote").animate({top: '100px'}, 500);
		}
		$(".quote").css("opacity",0).text("We design Characters, Storyboard and Animate.").animate({opacity:1},quoteDelay);
 	
 	});

 	
 	$("#motion").bind("mouseleave", function() {
 		$(".quote").css("opacity",0).text(defaultQuote).animate({opacity:1}, quoteDelay);
 		if (!isIpod) {
	 		$('#motion_animate').destroy();
	 		$('#motion_animate').css({backgroundPosition: '0px 0px'});

			
			setTimeout(function() {
	    		$("#motion_animate").removeClass();
			}, 500); 
		}
 	});
 	$("#web").bind("click mouseenter", function() {

		if (!isIpod) {
	 		$("#web_animate").addClass("to_front");
	 		$('#web_animate').destroy();
	 		$("#web_animate").sprite({
			    fps: 24, 
			    no_of_frames: 9,
			    on_first_frame: function(obj) {
			    	obj.spStart();
			    },
			    on_frame: { // note - on_frame is an object not a function
			        3: function(obj) {  
			            obj.spStop();  
			        }
			    },
			    on_last_frame: function(obj) {
	        		obj.spStop(); // stop the animation on the last frame
	    		}
	
			});
		}
		else {
			$("#interactive").animate({top: '20px'}, 400);

			$(".quote").animate({top: '100px'}, 500);
		}
		$(".quote").css("opacity",0).text("We make Beautiful and Functional Websites.").animate({opacity:1},quoteDelay);
 	
 	});

 	
 	$("#web").bind("mouseleave", function() {
 		$(".quote").css("opacity",0).text(defaultQuote).animate({opacity:1}, quoteDelay);
 		if (!isIpod) {
	 		$('#web_animate').destroy();
	 		$("#web_animate").sprite({
			    fps: 24, 
			    no_of_frames: 9, 
			    start_at_frame: 3,
			    on_last_frame: function(obj) {
	        		obj.spStop(); // stop the animation on the last frame
	    		}
			});
			
			setTimeout(function() {
	    		$("#web_animate").removeClass();
			}, 500); 
		}
 	});
	
	jQuery.fn.wave = function(reverse, offset, speed) {
			
		var object = $(this);
		
		var orig_xpos = parseInt($(this).css("left")); 
		
		var xpos = orig_xpos; 
		setInterval(function(){
			if (reverse) {
				xpos -= 1;
				if (xpos == (orig_xpos - offset)) reverse = false;
			}
			else {
				xpos += 1;
				if (xpos == (orig_xpos + offset)) reverse = true;
			} 
			 
			object.css('left', xpos.toString()+"px");
		}, speed)
	
	}
	
	$("#wave1").wave(true, 70, 120);
	$("#wave2").wave(true, 70, 200);
	$("#wave3").wave(false, 70, 100);
	$("#web").wave(true,15,210);
	
	$(".content-list-item-link").bind('mouseenter', function() {
	  $(this).find(".icon").css("display", "none");
	
	});
	$(".content-list-item-link").bind('mouseleave', function() {
	  $(this).find(".icon").css("display", "block");
	 
	});
	

	$('#contact_form').submit( submitForm );
	var errorMsg = "Please fill out this field.";

	$(':input','#contact_form').not(':button, :submit, :reset, :hidden').bind("click", function() {
	
		if ($(this).val() == errorMsg) {
			$(this).val("");
			$(this).removeClass("errorMsg");
		}
	});

	// Submit the form via Ajax

	function submitForm() {
	  var contact_form = $(this);
	
	  // Are all the fields filled in?
	
	  if (!$('#senderEmail').val() || !$('#message').val() ) {
	
	    // No; display a warning message and return to the form
	    $("#bee").fadeOut().removeClass().addClass("send_error").fadeIn();
	    $("#senderEmail, #message").each(function() {
	    
	    	if (!$(this).val()) {
	    	
	    		$(this).val(errorMsg);
	    		$(this).removeClass().addClass("errorMsg");
	    	}
	    });
	     
	  } else {
	
	    // Yes; submit the form to the PHP script via Ajax 
	    $.ajax( {
	      url: contact_form.attr( 'action' ) + "?ajax=true",
	      type: contact_form.attr( 'method' ),
	      data: contact_form.serialize(),
	      success: submitFinished
	    } );
	  }
	
	  // Prevent the default form submission occurring
	  return false;
	}


	// Handle the Ajax response	
	function submitFinished( response ) {
	  response = $.trim( response );
	  
	  if ( response == "success" ) {
	
	    // Form submitted successfully
	        		
		$(':input','#contact_form').not(':button, :submit, :reset, :hidden').val('');
		$("#bee").fadeOut().removeClass().addClass("send_thanks").fadeIn();
	
	  } else {
	
	    // Form submission failed: Display the failure message,
	    $("#bee").fadeOut().removeClass().addClass("send_error").fadeIn(); 
	     
	  }
	}	
});
