//################################################BLITZ_002.js##########################################################################
// BLITZ
var BLITZ = { };


$(function() {
	var PAGE_WIDTH = {
		SMALL: 1024,
		MEDIUM: 1600
	};

	// handle page resize
	$(window).resize(function() {
		var width = $(this).width();

		if(width < PAGE_WIDTH.SMALL)
		{
	        $("html").removeClass("medium large").addClass("small");
		}
		else if(width > PAGE_WIDTH.SMALL && width < PAGE_WIDTH.MEDIUM)
		{
			$("html").removeClass("small large").addClass("medium");
		}
		else if(width > PAGE_WIDTH.MEDIUM)
		{
			$("html").removeClass("small medium").addClass("large");
		}
	}).resize();
	
	$(".btn-hover").live("mouseover touchstart", function() { var $hover = $("> .hover", this); $hover.stop().fadeTo(200, 1, "easeOutQuad"); });
	$(".btn-hover").live("mouseout touchend", function() { var $hover = $("> .hover", this); $hover.stop().fadeTo(200, 0, "easeOutQuad"); });
});

// remove iPhone chrome on load
window.onload = function() { };

//######################################################################################################################################
(function($) {	
	$.fn.carousel = function(settings) {
		var config = {	
			btn_prev: 'btn-left',
			btn_next: 'btn-right',
			nav_items: '.carousel-nav-items li',
			viewable_nav_items: 5,
			carousel_items: '.carousel-items li',			
			interval: 9000,
			duration: 220,
			easing: "linear",
			enable_keys: false,
			current_class: "current",
			start_index: 0,
			is_mobile: false,
			webkit_easing: "cubic-bezier(0, 0, 0.2, 1)",
			carousel_transition_duration: 930,							 
			skin_header: ["skin-default", 
						  "skin-default", 
						  "skin-lightgray", 
						  "skin-darkgray", 
						  "skin-default", 
						  "skin-zoobles", 
						  "skin-default",
						  "skin-black"],
			skin_nav_items: ["skin-white", 
			 				 "skin-default", 
							 "skin-black", 
							 "skin-black", 
							 "skin-white", 
							 "skin-black", 
							 "skin-default",
							 "skin-black"]
		};

		if (settings) $.extend(config, settings);
		
		var set = 0,
			touch_event_threshold = 50;

		this.each(function(i) {	
			var $this = $(this),
				$btn_prev = $this.find(config.btn_prev),
				$btn_next = $this.find(config.btn_next),
				$nav_items = $this.find(config.nav_items),
				$nav_items_parent = $nav_items.parent(),
				$carousel_items = $("#carousel-items li"), 
				$carousel_items_pool = $("#carousel-items-pool"),
				$carousel_items_parent = $carousel_items.parent(),
				carousel_items_len = $carousel_items_pool.find("li").length, 
				timer = null;

			if(config.start_index > $nav_items.length)
			{
				config.start_index = 0;
			}

			config.new_current_index = config.start_index;
			config.index = config.start_index;

			// set parent width
			$nav_items_parent.width(get_nav_item_width($this) * $nav_items.length);
			
			if(!config.is_mobile)
			{			
				// start interval timer
				timer = setInterval(function() {
					$btn_next.click();
				}, config.interval);
			}
			
			
			// events
			$this.delegate(config.btn_prev, "click", function(evt) { // PREVIOUS BUTTON
				evt.preventDefault();
				
				if(!$this.data("is-moving"))
				{				
					var next_index = (config.index <= 0) ? carousel_items_len - 1 : config.index - 1;
					go_to($this, next_index, true);
				}
			}).delegate(config.btn_next, "click", function(evt) { // NEXT BUTTON
				evt.preventDefault();
				
				if(!$this.data("is-moving"))
				{
					var	next_index = (config.index >= carousel_items_len - 1) ? 0 : config.index + 1;
					go_to($this, next_index, true);
				}
			}).delegate(config.nav_items, "click", function(evt) {
				evt.preventDefault();
				
				if(!$this.data("is-moving"))
				{				
					go_to($this, $nav_items.index(this), true, false);				
				}
				
				clearInterval(timer);
			});
			
		
			
			// setup touch events for nav items			
			$carousel_items_parent.bind("touchstart", function(evt) {
				if(!$this.data("is-moving")) // disable touch events if carousel is moving
				{
				
					config.down_x = evt.originalEvent.touches[0].pageX,
					config.down_y = evt.originalEvent.touches[0].pageY;
					config.moved = false;
				
				
					var $carousel_item_middle = $carousel_items.eq(1),
						carousel_item_middle_className = $carousel_item_middle[0].className.replace("carousel-item", "").replace(" ", "");
						config.new_current_index = config.new_current_index || 0,
						config.index = config.new_current_index, 
						config.indicies = get_surrounding_indices($nav_items.index($(".carousel-nav-items li.current")[0]));

					$(this).bind("gesturestart", function() {
						config.gesturing = true; // sometimes crashes mobile safari when gesturing over carousel
						return false; // turned off default behavior by returning false
					}).bind("gestureend", function() {
						config.gesturing = false;
						return false;
					}).bind("touchmove", function(evt) {
						if(config.gesturing) {return false; }
					
						if(!config.moved)
						{
							var delta_y = evt.originalEvent.touches[0].pageY - config.down_y;
							/*
							if(Math.abs(delta_y) < 10)
							{
								//evt.preventDefault();
							}
							*/
						}
					
						config.moved = true;					
						config.up_x = evt.originalEvent.touches[0].pageX;					
						var xpos = config.up_x - config.down_x;

						$(this).css({
							"-webkit-transform": "translate3d(" + xpos + "px, 0, 0)"
						});
										
						//return false; // prevent scrolling up and down while over this element
					
					}).bind("touchend", function(evt) {
						$(this).unbind("gesturestart").unbind("gestureend").unbind("touchmove").unbind("touchend");
					
						if(config.moved) 
						{
							// get closest
							var threshold = 320, 
								xpos = 0,
								delta = config.down_x - config.up_x;							
								config.new_current_index = null;							
							
							if(delta < 0 && delta < -threshold) // slide right
							{
								xpos = 1024;
								config.new_current_index = config.indicies.prev;
							
							}
							else if(delta > 0 && delta > threshold) // slide left
							{
								xpos = -1024;
								config.new_current_index = config.indicies.next;							
							}						

							// slide and clean up
							$(this).css({	
								"-webkit-transition": "-webkit-transform 0.5s " + config.webkit_easing,						
								"-webkit-transform": "translate3d(" + xpos + "px, 0, 0)"
							}).bind("webkitTransitionEnd", function() {	
								$(this).unbind("webkitTransitionEnd");
								clearInterval(timer);								
								
								if(config.new_current_index != null)
								{	
									go_to($this, config.new_current_index, false, true);
									config.index = config.new_current_index;
									on_transition_end();		
								}
							});
						}
					
						//return false; // prevent scrolling up and down while over this element
					});
				
					// before move
					$carousel_items_parent.css({
						"-webkit-transition-duration": "0s",
						"-webkit-transition-property": "none"
					});
				}
			//	return false;
													
			})
		
		
			// setup touch events for carousel nav items
			$nav_items_parent.bind("touchstart", function(evt) {
				config.up_x = null;
				config.down_x = evt.originalEvent.touches[0].pageX;
			}).bind("touchmove", function(evt) {
				evt.preventDefault();
				config.up_x = evt.originalEvent.touches[0].pageX;
										
				var xpos = (config.up_x - config.down_x);
			
				// emulate elasticity when dragging
				if(config.up_x - config.down_x > touch_event_threshold && set == 0)
				{
					xpos = 50;
				}
				else if(set == 1)
				{
					// dragging right
					if((config.down_x - config.up_x) > touch_event_threshold)
					{
						xpos = -450;
					}
					else // dragging left
					{
						xpos = -360 + (config.up_x - config.down_x);
					}
				}

				
				$(this).css({
					"-webkit-transform": "translate3d(" + xpos + "px, 0, 0)"
				});
				
				
			}).bind("touchend", function() {
			
				if(config.down_x && config.up_x)
				{
					if((config.down_x - config.up_x) > touch_event_threshold)
					{					
						// slide right
						$nav_items_parent.css({
							"-webkit-transition": "-webkit-transform 0.5s " + config.webkit_easing,
							"-webkit-transform": "translate3d(-375px, 0, 0)"
						});
						set = 1;
					}
					else if((config.up_x - config.down_x) > touch_event_threshold)
					{
						// slide left
						$nav_items_parent.css({
							"-webkit-transition": "-webkit-transform 0.5s " + config.webkit_easing,
							"-webkit-transform": "translate3d(0, 0, 0)"
						});
						
						set = 0;
					}	
					else
					{
						if(set == 0)
						{
							$nav_items_parent.css({
								"-webkit-transition": "-webkit-transform 0.5s " + config.webkit_easing,
								"-webkit-transform": "translate3d(0, 0, 0)"
							});
						}
						else
						{
							$nav_items_parent.css({
								"-webkit-transition": "-webkit-transform 0.5s " + config.webkit_easing,
								"-webkit-transform": "translate3d(-375px, 0, 0)"
							});
						}
					}
				}
				
				config.up_x = null;
				config.down_x = null;
			});
			
			
			
			// key events
			if(config.enable_keys)
			{
				enable_key_events($this);
			}				
		});
		
		// go to item in carousel
		function go_to($this, index, is_click, is_slide)
		{
			var $nav_items = $this.find(config.nav_items),				
				$nav_items_parent = $nav_items.parent(),
				nav_items_length = $nav_items.length,
				nav_item_width = get_nav_item_width($this),
				nav_item_index = index,
				webkit_secs = config.duration / 1000,
				xpos;
			
			// hardcoding 2 sets (0 and 1)
			// is there a way to make this dynamic?
	
			if((set == 0 && nav_item_index < config.viewable_nav_items) || (set == 1 && nav_item_index < (nav_items_length - config.viewable_nav_items)))
			{
				nav_item_index = 0;
				set = 0;
			}
			else if(nav_item_index > nav_items_length - config.viewable_nav_items)
			{
				nav_item_index = nav_items_length - config.viewable_nav_items;
				set = 1;
			} 
			
			xpos = -1 * nav_item_width * nav_item_index;
		
			if(config.is_mobile)
			{
				//??? no need to realign carousel nav when dragging/sliding on touch device
				$nav_items_parent.css({
					"-webkit-transition": "-webkit-transform " + webkit_secs + "s " + config.webkit_easing,
					"-webkit-transform": "translate3d(" + xpos + "px, 0, 0)"
				});
			
				
			}
			else
			{
				$nav_items_parent.animate({
					"left": xpos
				}, config.duration, config.easing);
			}

			set_current($this, index, is_click, is_slide);
		}
		
		function get_nav_item_width($this)
		{
			return $this.find(config.nav_items).outerWidth(true);
		}
		
		// set current item in carousel
		function set_current($this, index, is_click, is_slide)
		{
			var $nav_items = $this.find(config.nav_items),
				$current_nav_item = get_current_nav_item($this);
			

			if(config.index != index)
			{
				$this.data("is-moving", true);
				
							
				// add current class to index, remove it from siblings
				$nav_items.eq(index).addClass(config.current_class);
				$current_nav_item.removeClass(config.current_class);								
												
				if(!is_slide)
				{			
					// go to next
					var xpos = -3200,
						webkit_transition = -1024,
						webkit_secs = config.carousel_transition_duration / 1000,
						$carousel_items = $("#carousel-items li"),
						$carousel_items_parent = $("#carousel-items"),
						$carousel_items_pool = $("#carousel-items-pool"),
						carousel_items_len = $carousel_items_pool.find("li").length;			
				
					config.indicies = get_surrounding_indices(config.index);
					config.new_current_index = index;
				
					if(is_click)
					{
						config.indicies.next = index;	
					}
				
					// go to prev									
					if((config.index == 0 && index == carousel_items_len - 1) || config.index > index)
					{
						xpos = 0;
						webkit_transition = 1024;
						if(is_click)
						{
							config.indicies.prev = index;
						}
					}
				
					// go to next
					if(config.index == carousel_items_len - 1 && index == 0)
					{
						xpos = -3200;
						webkit_transition = -1024;
						if(is_click)
						{
							config.indicies.next = index;
						}
					}
				
					//console.log(xpos, config.indicies.prev, config.indicies.next, config.index, index, carousel_items_len);
				
					var $carousel_item_prev = $carousel_items_pool.find("li:eq(" + config.indicies.prev + ")");
					var $carousel_item_next = $carousel_items_pool.find("li:eq(" + config.indicies.next + ")");
								
					//$carousel_items.eq(0)[0].outerHTML = $carousel_item_prev[0].outerHTML;
					//$carousel_items.eq(2)[0].outerHTML = $carousel_item_next[0].outerHTML;
					$carousel_items.eq(0).outerHTML($carousel_item_prev.outerHTML());
					$carousel_items.eq(2).outerHTML($carousel_item_next.outerHTML());
					
					//$carousel_items.eq(0).remove().parent().prepend($('<div>').append($carousel_item_prev.clone()).html());
					//$carousel_items.eq(2).remove().parent().append($('<div>').append($carousel_item_next.clone()).html());
					
					
					if(config.is_mobile)
					{
						$carousel_items_parent.css({
							"-webkit-transition": "-webkit-transform 0.5s " + config.webkit_easing,						
							"-webkit-transform": "translate3d(" + webkit_transition + "px, 0, 0)"
						}).bind("webkitTransitionEnd", function() {			
							on_transition_end();
						});											
					}
					else
					{
						config.index = index;					
						$carousel_items_parent.animate({
							"margin-left": xpos
						}, config.carousel_transition_duration, "easeOutQuad", function() {						
							on_transition_end();
						});
					
					}					
				}
			
				
				config.index = index;
					
				// clean up once animation is complete	
				var timer = setTimeout(function() {
					$this.data("is-moving", false);	
					//$this.trigger("change");
					clearTimeout(timer);
				}, config.carousel_transition_duration);
			
				$this.trigger("change");
				change_skin($this, config.new_current_index);
				
			}
			
		}
		
		function on_transition_end()
		{			
			var indicies = get_surrounding_indices(config.index),
				$carousel_items_pool = $("#carousel-items-pool"),
				$carousel_items_parent = $("#carousel-items"),
				//$carousel_item_prev = $carousel_items_pool.find("li:eq(" + indicies.prev + ")"),
				//$carousel_item_next = $carousel_items_pool.find("li:eq(" + indicies.next + ")"),
				//$carousel_item_current = $carousel_items_pool.find("li:eq(" + indicies.new_current_index + ")"),
				//carousel_item_prev_html = $('<div>').append($carousel_item_prev.clone()).html(),
				//carousel_item_next_html = $('<div>').append($carousel_item_next.clone()).html(),
				//carousel_item_current_html = $('<div>').append($carousel_item_current.clone()).html(),
				carousel_item_prev_html = $carousel_items_pool.find("li:eq(" + indicies.prev + ")").outerHTML(),
				carousel_item_next_html = $carousel_items_pool.find("li:eq(" + indicies.next + ")").outerHTML(),
				carousel_item_current_html = $carousel_items_pool.find("li:eq(" + config.new_current_index + ")").outerHTML(),						
				items = [carousel_item_prev_html, carousel_item_current_html, carousel_item_next_html];

			var recenter_css = config.is_mobile ? {"-webkit-transform": "translate3d(0, 0, 0)"} : {"margin-left": -1600};													
			$carousel_items_parent.html(items.join("")).css(recenter_css);

			if(config.is_mobile)
			{
				$carousel_items_parent.unbind("webkitTransitionEnd").css({
					"-webkit-transition": "none"
				});
			}
		}
		
		function get_surrounding_indices(index)
		{
			var max = $("#carousel-items-pool").find("li").length - 1, //$this.find(config.carousel_items).length - 1,
				next = index + 1,
				prev = index - 1;
			
			if(next > max)
			{
				next = 0;
			}
			
			if(prev < 0)
			{
				prev = max;
			}
			
			return {"next": next, "prev": prev};
		}
		
		function change_skin($this, index)
		{
			var $carousel = $this,
				$wrapper = $(".wrapper:first");
			
			// remove skin class
			$carousel[0].className = $carousel[0].className.replace(/skin-.*/g, "");
			//$wrapper[0].className = $wrapper[0].className.replace(/skin-.*/g, "");
			
			// add skin class
			$carousel.addClass(config.skin_nav_items[index]);
			$wrapper.addClass(config.skin_header[index]);
			
			//Cufon.refresh();
		}
		
		function get_current_nav_item($this)
		{
			return $this.find(config.nav_items + "." + config.current_class);
		}
			
		function enable_key_events($this)
		{			
			$(window).keydown(function(evt) {
				var key_code = evt.keyCode || evt.which;

				switch(key_code)
				{
					// left
					case 37:
						$this.find(config.btn_prev).click();
						break;
					// up
					case 38:
						break;
					// right
					case 39:
						$this.find(config.btn_next).click();
						break;
					// down
					case 40:
						break;
				}
			});
		}
		
		return this;
	};
})(jQuery);

// cross browser outerHTML
jQuery.fn.outerHTML = function(val) {
	if(val)
	{
		$(val).insertBefore(this);
		$(this).remove();
	}
	else
	{
    	return $('<div>').append( this.eq(0).clone() ).html();
	}
};
//######################################################################################################################################
//##############################################BILTZ_004.js############################################################################
$(function() {
	$("#carousel").carousel({
		btn_prev: ".btn-arrow-left",
		btn_next: ".btn-arrow-right",
		easing: "easeInOutQuad",
		duration: 500,
		viewable_nav_items: 5,
		enable_keys: false//,
	}).change(function(evt, carousel_item) { });
	
	$.fn.duplicate = function(count, cloneEvents) {
		var tmp = [];
		for ( var i = 0; i < count; i++ ) {
			$.merge( tmp, this.clone( cloneEvents) );
		}
		return this.pushStack( tmp );
	};
});

BLITZ.marquee = {
	create_mkz_stars: function()
	{		
        var $star = $(".star"), num_stars = 15;
        $star.duplicate(num_stars).insertAfter($star).each(function(i) {
        	$(this).css({
        		"left": Math.random() * 1024,
        		"top": 150 + (Math.random() * 275)
        	}).addClass("star-" + i);
        });
        $star.eq(0).remove();
	},
};
//######################################################################################################################################

