/**
 * touch for jQuery
 * 
 * Copyright (c) 2008 Peter Schmalfeldt (ManifestInteractive.com) <manifestinteractive@gmail.com>
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details. 
 *
 * @license http://www.gnu.org/licenses/gpl.html 
 * @project jquery.touch
*
*
* This script was WIDELY changed by George Willian Condomitti
*/

// DEFINE DEFAULT VARIABLES
var _target=null, _dragx=null, _dragy=null, _rotate=null, _resort=null;
var _dragging=false, _sizing=false, _animate=false;
var _rotating=0, _width=0, _height=0, _left=0, _top=0, _xspeed=0, _yspeed=0,_maxLeft=0;
var _zindex=1000;
var initX;
var increased = false;
var PIXEL_UNIT_PER_SLIDE = 556 / 144;
var movement;
var toShrink;

jQuery.fn.touch = function(settings) {

	// DEFINE DEFAULT TOUCH SETTINGS
	settings = jQuery.extend({
		animate: true,
		sticky: false,
		dragx: true,
		dragy: true,
		rotate: false,
		resort: true,
		scale: false,
		maxLeft : 0
	}, settings);
	
	// BUILD SETTINGS OBJECT
	var opts = [];
	opts = $.extend({}, $.fn.touch.defaults, settings);

	// ADD METHODS TO OBJECT
	this.each(function(){
		this.opts = opts;
		this.ontouchstart = touchstart;
		this.ontouchend = touchend;
		this.ontouchmove = touchmove;
		this.ongesturestart = gesturestart;
		this.ongesturechange = gesturechange;
		this.ongestureend = gestureend;

	});



};

function touchstart(e){
	_target = this.id;
	_dragx = this.opts.dragx;
	_dragy = this.opts.dragy;
	_resort = this.opts.resort;
	_animate = this.opts.animate;
	_xspeed = 0;
	_yspeed = 0;
	_maxLeft = this.opts.maxLeft;

	clearInterval(fncTmOut);
	clearTimeout(fncTmOut);

	refreshSlides();

	$(e.changedTouches).each(function(){


		var curLeft = ($('#'+_target).css("left") == 'auto') ? this.pageX : parseInt($('#'+_target).css("left"));
		var curTop = ($('#'+_target).css("top") == 'auto') ? this.pageY : parseInt($('#'+_target).css("top"));
		
		if(!_dragging && !_sizing){
			_left = (e.pageX - curLeft);
			_top = (e.pageY - curTop);
			_dragging = [_left,_top];
			if(_resort){
				_zindex = ($('#'+_target).css("z-index") == _zindex) ? _zindex : _zindex+1;
				$('#'+_target).css({ zIndex: _zindex });
			}
		}
	});
	initX = parseInt($('#bottom_banner_current_item').css("margin-left"));
	$('.centralBannerContentBlock').hide();


};

function touchmove(e){
	
	if(_dragging && !_sizing && _animate) {
		
		var _lastleft = (isNaN(parseInt($('#'+_target).css("left")))) ? 0:parseInt($('#'+_target).css("left"));
		var _lasttop = (isNaN(parseInt($('#'+_target).css("top")))) ? 0:parseInt($('#'+_target).css("top"));
	}
	
	$(e.changedTouches).each(function(){
		
		e.preventDefault();
		
		//_left = (this.pageX-(parseInt($('#'+_target).css("width"))/2));
		_left = (this.pageX-(parseInt($('#'+_target).css("width"))));
		_top = (this.pageY-(parseInt($('#'+_target).css("height"))/2));
		if(_dragging) {
			if(_left <= 0) _left = 1;
			else if(_left > _maxLeft) _left = _maxLeft;
		} else {
			if(_left < 0) _left = 0;
			else if(_left > _maxLeft) _left = _maxLeft;
		}
		
		if(_dragging && !_sizing) {
			
			if(_animate){
				_xspeed = Math.round((_xspeed + Math.round( _left - _lastleft))/1.5);
				_yspeed = Math.round((_yspeed + Math.round( _top - _lasttop))/1.5);
			}

			$('#'+_target).css({ 'margin-left': _left+"px" })
			
			if(_dragx || _dragy) $('#'+_target).css({ position: "absolute" });
			if(_dragx) $('#'+_target).css({ 'margin-left': _left+"px" });
			if(_dragy) $('#'+_target).css({ top: _top+"px" });


			if(isIncreasing()){

				if(current_slide != Math.floor(parseInt($('#' + _target).css('margin-left')) / 144) + 1) initX = parseInt($('#' + _target).css('margin-left'));
				current_slide = Math.floor(parseInt($('#' + _target).css('margin-left')) / 144) + 1;

				$('#mascoteImg' + (parseInt(current_slide)-1)).css('z-index', 0);
				
				$('#mascoteImg' + current_slide).css('z-index',500);
				$('#content' + current_slide).css('z-index',501);

				$('#mascoteImg' + (parseInt(current_slide)+1)).css('z-index',498);
				$('#content' + (parseInt(current_slide)+1)).css('z-index',499);
				$('#mascoteImg' + (parseInt(current_slide)+1)).css('width',556);
				$('#mascoteImg' + (parseInt(current_slide)+1)).show();


				movement = parseInt($('#' + _target).css('margin-left')) - initX;
				toShrink = PIXEL_UNIT_PER_SLIDE * movement;

				$('#mascoteImg' + current_slide).show();
				$('#mascoteImg' + current_slide).css('width',556 - toShrink);
				

			} else {


				$('.mascote').css('z-index',0);
				$('.mascote').hide();

				if(current_slide != Math.floor(parseInt($('#' + _target).css('margin-left')) / 144) + 1) initX = parseInt($('#' + _target).css('margin-left'));
				current_slide = Math.floor(parseInt($('#' + _target).css('margin-left')) / 144) + 1;


				$('#mascoteImg' + current_slide).css('z-index',500);
				$('#content' + current_slide).css('z-index',501);
				$('#mascoteImg' + current_slide).css('width',0);
				$('#mascoteImg' + current_slide).show();

				$('#mascoteImg' + (parseInt(current_slide)+1)).css('z-index',498);
				$('#content' + (parseInt(current_slide)+1)).css('z-index',499);
				$('#mascoteImg' + (parseInt(current_slide)+1)).css('width',556);
				$('#mascoteImg' + (parseInt(current_slide)+1)).show();


				movement = initX - parseInt($('#' + _target).css('margin-left'));
				toWide = PIXEL_UNIT_PER_SLIDE * movement;

				if(toWide <= 0) toWide = PIXEL_UNIT_PER_SLIDE;

				$('#mascoteImg' + current_slide).css('width',toWide);

			}
		//checkZIndex();
		}


	});
}


function checkZIndex(){
	$('#footer').text(
		'img1: ' + $('#mascoteImg1').css('z-index') + ' \/' +
		'img2: ' + $('#mascoteImg2').css('z-index') + ' \/' +
		'img3: ' + $('#mascoteImg3').css('z-index') + ' \/' +
		'CS:  ' + current_slide + '\/' +
		'CS_W: ' + $('#mascoteImg' + current_slide).css('width')
	);
}

function refreshSlides(){
	$('.mascote').each(function(){
		if($(this).attr('id') != 'mascoteImg' + current_slide){
			$(this).css('z-index',0);
		}
	});
}

function isIncreasing(){
	increased = parseInt($('#bottom_banner_current_item').css("margin-left")) > initX;
	return increased;
}

function touchend(e){
	$(e.changedTouches).each(function(){
		if(!e.targetTouches.length){
			_dragging = false;
			if(_animate){
				//_left = ($('#'+_target).css("left") == 'auto') ? this.pageX : parseInt($('#'+_target).css("left"));
				//_top = ($('#'+_target).css("top") == 'auto') ? this.pageY : parseInt($('#'+_target).css("top"));
				
				var animx = (_dragx) ? (_left+_xspeed)+"px" : _left+"px";
				var animy = (_dragy) ? (_top+_yspeed)+"px" : _top+"px";
				
				if(_dragx || _dragy) $('#'+_target).animate({ 'margin-left': animx }, "fast");
			}
			var indicators = $('.bottom_banner_indicator').length;
			for(var i = 0 ; i < indicators ; i++){
				var objLeft = parseInt($('#' + _target).css('margin-left'));
					if(objLeft < 0) objLeft = 0;
					else if(objLeft > _maxLeft) objLeft = _maxLeft;
				if(objLeft < (i+1) * 144 - 72){
					$('#' + _target).animate({'margin-left' : (i * 144)},
											function(){
												refreshSlides();
											}
					);

					//It's still positioned at the current slide's universe
					if(current_slide == (i+1)){
						$('#mascoteImg' + current_slide).animate({ 'width' : 556 });
						if(!increased) $('#mascoteImg' + (parseInt(current_slide) - 1)).animate({ 'width' : 0}, function(){  });
					//At the next slide's universe
					}else{
						$('#mascoteImg' + (parseInt(current_slide) - 1)).animate({ 'width' : 556 });
						if(increased) $('#mascoteImg' + current_slide).animate({ 'width' : 0 }, function(){ });
					}
					current_slide = i+1;
					break;
				}
			}

			$('#content' + current_slide).fadeIn(500);
//			$('#content' + current_slide).show();
		}
	});
	fncTmOut = setTimeout("autoChangeBanner();",CENTRAL_BANNER_TRANSITION_TIME);
	
}


function gesturestart(e){
/*	_sizing = [$('#'+this.id).css("width"), $('#'+this.id).css("height")];*/
}

function gesturechange(e){
	if(_sizing){
		_width = (this.opts.scale) ? Math.min(parseInt(_sizing[0])*e.scale, 300) : _sizing[0];
		_height = (this.opts.scale) ? Math.min(parseInt(_sizing[1])*e.scale, 300) : _sizing[1];
		_rotate = (this.opts.rotate) ? "rotate(" + ((_rotating + e.rotation) % 360) + "deg)" : "0deg";		
		$('#'+this.id).css({ width: _width+"px", height: _height+"px", webkitTransform: _rotate });
		$('#'+this.id+' b').text('TRANSFORM!');
	}
}

function gestureend(e){
	_sizing = false;
	_rotating = (_rotating + e.rotation) % 360;
}

