/*
 * jQuery Custom Flow Plugin.
 *
 * Copyright (c) 2011-2012 WeDig Technologies
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * @author: Pranay Mathur
 * @version: 0.10 (31-JAN-2010)
 * @requires jQuery v1.4.x or later
 *
 */
 
 if(!Array.indexOf){
  Array.prototype.indexOf = function(obj){
   for(var i=0; i<this.length; i++){
    if(this[i]==obj){
     return i;
    }
   }
   return -1;
  }
}





$().ready (function() {
	var isStarted = 0;
	
	$('.categoryName').live('click', function() {
		//alert($(this).find('img').position().left);
		var offset = $(this).find('img').position();
		leftOffset = offset.left;

		var jsCustomId = $(this).attr('data-id');
		$('#galleryBox').find('.customFlow').hide();
		$('#galleryBox').find('#'+jsCustomId).fadeIn(800);

		if( isStarted == 0 && $('.gallery_logo').css('display') == 'none' ) {
			$('#'+jsCustomId).find('.thumbs:eq('+(numItems-1)+')').trigger('click');	
			isStarted = 1;
		}

		$('#'+jsCustomId).jqCustomFlow( { circular: true });
		var leftOffset = parseInt(leftOffset);


		if( $('.gallery_logo').css('display') == 'none' ) {
			numItems = $('#'+jsCustomId).find('.thumbs').length;
			$('#'+jsCustomId).find('.thumbs:eq('+(numItems-1)+')').trigger('click');	
		}


		$('.categoryBox').find('.white_arrow').animate({'left':leftOffset+'px'},{'easing':'easeout'});
		
	});
	$('.categoryName:first').trigger('click');
	
	 if($.client.browser == 'Firefox') {
		setTimeout("showLogoTxt()", 3500);
	} else {
		setTimeout("showLogoTxt()", 3800);
	}
	
	
	
});

function showLogoTxt() {
	$(".logo_text").fadeIn(700);
}















 
 
(function( $ ){


	$.fn.jqCustomFlow = function(options) {
		
		if( $.fn.jqCustomFlow.defaults['allObjects'].indexOf($(this).attr('id')) > -1  ) {
			return false;	
		}
		
		$.fn.jqCustomFlow.defaults['allObjects'][$.fn.jqCustomFlow.defaults['objectId']] = $(this).attr('id');
		
		//alert( $.fn.jqCustomFlow.defaults['allObjects'].toSource() );
		
		
		var d = $.fn.jqCustomFlow.defaults;
		
		var opts = $.extend({}, d, options);
		
		var horDistance = 10;
		var verDistance = 0;
		var objCustomFlow = $(this);
		var i=0;
		
		$.fn.jqCustomFlow.defaults['lastItemId'] = 0;
		
		$.fn.jqCustomFlow.defaults['objectId']++;
		$(this).attr('jqCustomId', $.fn.jqCustomFlow.defaults['objectId']);
		

		
		$(this).find('>ul li').each( function() {
			$(this).css('display', 'block');
			$(this).find('img').css({width:'100px', height:'100px'});
			
			
			$(this).addClass('thumbs');
			$(this).attr('data-id', 'custom_'+objCustomFlow.attr('jqCustomId')+'_thumbs'+$.fn.jqCustomFlow.defaults['lastItemId']);
			
			$.fn.jqCustomFlow.defaults['lastItemId']++;

			$(this).css('left', parseInt(horDistance)+'px' );
			$(this).css('top', parseInt(verDistance)+'px' );
			
			horDistance = horDistance + opts.thumbsHorizontalDistance;
			verDistance = verDistance + opts.thumbsVerticalDistance;
		});

		numItems = $(this).find('.thumbs').length;

		$(this).find('.thumbs:eq('+(numItems-1)+')').live('click', function() {
			$.fn.jqCustomFlow.focusItem(this, opts, objCustomFlow);						
		});
		
		$(this).find('.playButton').live('click', function() {
			$.fn.jqCustomFlow.play(this, opts, objCustomFlow);						
		});
		
		if( !$.fn.jqCustomFlow.defaults['autoPlay'] ) {
				//alert($('.gallery_logo').css('display'));
			//alert ($('.gallery_logo').css('display'));
			if( $('.gallery_logo').css('display') == 'none' ) {
				$(this).find('.thumbs:eq('+(numItems-1)+')').trigger('click');	
			}
		} else {
			$.fn.jqCustomFlow.play($(this).find('.playButton'), opts, objCustomFlow);
			$(this).find('.playButton').find('img').attr('src', 'modules/jquery-flash/img/pause.jpg');
		}
		
		
		
		//$(this).find('.customFlowContent div').each( function(index) {
																	  
		//});
		
	};
	
	$.fn.jqCustomFlow.focusItem = function(obj, opts, objCustomFlow) {
		if($.fn.jqCustomFlow.defaults['animationInProgress'] > -1) {
			
			$('.gallery_logo').hide();
			
			lastitemleft = parseInt( objCustomFlow.find('.thumbs:eq(0)').css('left'));
			lastitemtop = parseInt(objCustomFlow.find('.thumbs:eq(0)').css('top'));
			
			/* Create copy of the current element and prepend to the list for circular flow */
			var thumbsContainer = objCustomFlow.find('.thumbsContainer');
			objNew = $(obj).clone().css('display', 'block').prependTo(thumbsContainer).css( {
																							'left': (lastitemleft-opts.thumbsHorizontalDistance)+'px',
																							'top': (lastitemtop-opts.thumbsVerticalDistance)+'px',
																							'margin-left': '0px',
																							'margin-top': '0px'
																						   }).removeClass('current');
			/*$.fn.jqCustomFlow.defaults['lastItemId']++;
			newItemId = $(objNew).attr('id');
			var mrotate = "rotate(10deg, -15deg)";
			//cssSandpaper.setTransform( document.getElementById(newItemId), mrotate);
	*/		
			//$(obj).removeClass('thumbs');
			objCustomFlow.find('.current').fadeOut(200);
			objCustomFlow.find('.current').remove();
			objCustomFlow.find('.current').addClass('newItem');
			objCustomFlow.find('.customFlowContent>div').fadeOut(200);
			
			
			$(obj).removeClass('thumbs');
			$.fn.jqCustomFlow.defaults['animationInProgress'] = 1;
			$(obj).find('img').animate({
								'width': '200px',
								'height': '200px'
						   }, opts.speed, function(){
								// Reposition all the thumbs
								$.fn.jqCustomFlow.repositionThumbs(opts, objCustomFlow);
								
						   });
			$(obj).animate({ 
								'width': '200px',
								'height': '200px',
								'left': '150px',
								'margin-left': '0px',
								'border': '0px',
								'top': '-70px'
								}, opts.speed, function() {
									$.fn.jqCustomFlow.defaults['animationInProgress'] = 0;	
								});

			$(obj).animate({ 
				'width': '300px',
				'height': '300px',
				'left': '155px',
				'margin-left': '0px',
				'border': '0px',
				'top': '-70px'
				}, opts.speed, function() {
					$.fn.jqCustomFlow.defaults['animationInProgress'] = 0;	
			});

			$(obj).addClass('current');
			itemId = $(obj).attr('data-id').replace('custom_'+objCustomFlow.attr('jqCustomId')+'_thumbs','');
			objCustomFlow.find('.customFlowContent>div:eq('+itemId+')').fadeIn(400);
		}
	}
	
	
	
	/* Reposition Thumbs */
	$.fn.jqCustomFlow.repositionThumbs = function (opts, objCustomFlow) {
		
		lastItemLeft = parseInt(objCustomFlow.find('.thumbs:first').css('left')); 
		lastItemTop = parseInt(objCustomFlow.find('.thumbs:first').css('margin-top')); 
		
		objCustomFlow.find('.thumbs').each( function(index) {
			itemNum = $(this).attr('data-id').replace('custom_'+objCustomFlow.attr('jqCustomId')+'_thumbs','');
			imarginleft = parseInt($(this).css('left'));
			imargintop = parseInt($(this).css('top'));
			$(this).animate({ 
							'left': imarginleft+opts.thumbsHorizontalDistance+'px',
							'top': imargintop+opts.thumbsVerticalDistance+'px'
							
							}, {duration: 300});		 
		});
	}
	
	
	/* Auto Play */
	$.fn.jqCustomFlow.play = function (thisObj, opts, objCustomFlow) {
		if( opts['isPlaying'] == 0 ) {
			opts['isPlaying'] = 1;
			$(thisObj).find('img').attr('src', 'modules/jquery-flash/img/pause.jpg');
			$.fn.jqCustomFlow.keepPlaying(opts, objCustomFlow);
		} else {
			$(thisObj).find('img').attr('src', 'modules/jquery-flash/img/play.jpg');
			opts['isPlaying'] = 0;	
		}
	}
	
	
	/* Keep Playing */
	$.fn.jqCustomFlow.keepPlaying = function (opts, objCustomFlow) {
		//alert( opts);
		if( opts['isPlaying']== 1 ) {
			if( objCustomFlow.css('display') != 'none' ) {
				numItems = objCustomFlow.find('.thumbs').length;
				objCustomFlow.find('.thumbs:eq('+(numItems-1)+')').trigger('click');
			}
			setTimeout( function() {$.fn.jqCustomFlow.keepPlaying(opts, objCustomFlow);}, opts['playDuration'] );
		}
	}
	
	$.fn.jqCustomFlow.defaults = {
		thumbsHorizontalDistance: 10,
		thumbsVerticalDistance: 5,
		objectId: 0,
		startItem: 1,
		currentImage: 1,
		speed: 240,
		autoPlay: false,
		playDuration: 3500,
		circular: true,
		lastItemId: 0,
		animationInProgress: 0,
		isPlaying: 0,
		objectId: 0,
		allObjects: new Array()
	};
	
	if (!$.easing.easeout) {
		$.easing.easeout = function(p, n, firstNum, delta, duration) {
			return -delta * ((n=n/duration-1)*n*n*n - 1) + firstNum;
		};
	}
	
  
  
  
})( jQuery );

