/*
	Author: The Bridge
	Copyright (c): 2011 The Bridge
	Created: 15-11-2011
	Last Modified: 20-01-2012
	Name: Slider
	Description: Content slider for modal content
*/

(function($) {
	$.fn.bridgeSlider = function(options){
		var defaults = {			
			prevId: 'sliderprev',
			prevText: '',
			nextId: 'slidernext',	
			nextText: '',
			controlsShow: true,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	true,
			firstId: 'firstBtn',
			firstText: 'First',
			firstShow: false,
			lastId:  'lastBtn',	
			lastText: 'Last',
			lastShow: false,				
			vertical: false,
			speed: 800,
			auto: false,
			pause: 2000,
			continuous: false, 
			numeric: false,
			numericId: 'controls',
			media: 'screen'
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this); 				
			var s = $("li", obj).length;
			var w = $("li", obj).width(); 
			var h = $("li", obj).height(); 
			var clickable = true;
			var idBits = $("li", obj).eq(0).find("img").attr("id").split("_");
			var altBits = $("li", obj).eq(0).find("img").attr("alt").split(" - ");
			var clientName = idBits[1].replace(/-/g, ' ');

			$("#h4_modal").text(clientName);
			if (idBits[0] == 'web') $("#p_modal").html('<a href="http://'+altBits[2]+'" title="View site in new window" target="_blank">View live site</a>');
			
			obj.width(w); 
			obj.height(h); 
			obj.css("overflow","hidden");
			
			var ts = s-1;
			var t = 0;
			
			$("ul", obj).css('width', s*w);			
			
			if(options.continuous){
				$("ul", obj).prepend($("ul li:last-child", obj).clone().css("margin-left","-"+ w +"px"));
				$("ul", obj).append($("ul li:nth-child(2)", obj).clone());
				$("ul", obj).css('width',(s+1)*w);
			};				
			
			if(!options.vertical) $("li", obj).css('float','left');
			
			$("li", obj).each(function(){
				$(this).click(function(){ imodal($('img', $(this)).attr("src"), $('img', $(this)).attr("id")); });
			});
								
			if(options.controlsShow){
				var html = options.controlsBefore;				
				if(options.numeric){
					html += '<ol id="'+ options.numericId +'"></ol>';
				} else {
					if(options.firstShow) html += '<span id="'+ options.firstId +'">'+ options.firstText +'</span>';
					html += ' <span id="'+ options.prevId +'">'+ options.prevText +'</span>';
					html += ' <span id="'+ options.nextId +'">'+ options.nextText +'</span>';
					if(options.lastShow) html += ' <span id="'+ options.lastId +'">'+ options.lastText +'</span>';				
				};
				
				html += options.controlsAfter;
				$(obj).parent().parent().append(html);										
			};
			
			if(options.numeric){									
				for(var i=0;i<s;i++){						
					$(document.createElement("li"))
						.attr('id',options.numericId + (i+1))
						.html('<a rel='+ i +' href=\"javascript:void(0);\">'+ (i+1) +'</a>')
						.appendTo($("#"+ options.numericId))
						.click(function(){							
							animate($("a",$(this)).attr('rel'),true);
						}); 												
				};							
			} else {
				$("#"+options.nextId).hover(
					function(){
						$(this).css( { backgroundPosition : '-35px 0' } );
					},
					function(){
						$(this).css( { backgroundPosition : '0 0' } );
					}
				).click(function(){		
					animate("next",true);
				});
				$("#"+options.prevId).hover(
					function(){
						$(this).css( { backgroundPosition : '-35px 0' } );
					},
					function(){
						$(this).css( { backgroundPosition : '0 0' } );
					}
				).click(function(){		
					animate("prev",true);				
				});	
				$("#"+options.firstId).click(function(){		
					animate("first",true);
				});				
				$("#"+options.lastId).click(function(){		
					animate("last",true);				
				});				
			};
			
			function setCurrent(i){
				i = parseInt(i)+1;
				$("li", "#" + options.numericId).removeClass("current");
				$("li#" + options.numericId + i).addClass("current");
			};
			
			function adjust(){
				if(t>ts) t=0;		
				if(t<0) t=ts;	
				if(!options.vertical) {
					$("ul",obj).css("margin-left",(t*w*-1));
				} else {
					$("ul",obj).css("margin-left",(t*h*-1));
				}
				clickable = true;
				if(options.numeric) setCurrent(t);
			};
			
			function animate(dir,clicked){
				if (clickable){
					clickable = false;
					var ot = t;				
					switch(dir){
						case "next":
							t = (ot>=ts) ? (options.continuous ? t+1 : ts) : t+1;						
							break; 
						case "prev":
							t = (t<=0) ? (options.continuous ? t-1 : 0) : t-1;
							break; 
						case "first":
							t = 0;
							break; 
						case "last":
							t = ts;
							break; 
						default:
							t = dir;
							break; 
					};	
					
					var diff = Math.abs(ot-t);
					var speed = diff*options.speed;	
					var nthchild = t+1;
					var idBits = $("li", obj).eq(nthchild).find("img").attr("id").split("_");
					var altBits = $("li", obj).eq(nthchild).find("img").attr("alt").split(" - ");
					var clientName = idBits[1].replace(/-/g, ' ');
			
					$("#h4_modal").text(clientName);
					if (idBits[0] == 'web') $("#p_modal").html('<a href="http://'+altBits[2]+'" title="View site in new window" target="_blank">View live site</a>');

									
					if(!options.vertical) {
						p = (t*w*-1);
						$("ul",obj).animate(
							{ marginLeft: p }, 
							{ queue:false, duration:speed, complete:adjust }
						);				
					} else {
						p = (t*h*-1);
						$("ul",obj).animate(
							{ marginTop: p }, 
							{ queue:false, duration:speed, complete:adjust }
						);					
					};

					if(!options.continuous && options.controlsFade){					
						if(t==ts){
							$("a","#"+options.nextId).hide();
							$("a","#"+options.lastId).hide();
						} else {
							$("a","#"+options.nextId).show();
							$("a","#"+options.lastId).show();					
						};
						if(t==0){
							$("a","#"+options.prevId).hide();
							$("a","#"+options.firstId).hide();
						} else {
							$("a","#"+options.prevId).show();
							$("a","#"+options.firstId).show();
						};					
					};				
					
					if(clicked) clearTimeout(timeout);
					if(options.auto && dir=="next" && !clicked){;
						timeout = setTimeout(function(){
							animate("next",false);
						},diff*options.speed+options.pause);
					};
			
				};
				
			};
			
			function imodal(imgSrc, liId){
				var srcBits = imgSrc.split('/');
				var fileBits = srcBits[srcBits.length-1].split('.');
				var largeFile = fileBits[0]+'_large.jpg';
				var idBits = liId.split('_');
				var iWidth = 860;
				var iHeight = 795;
				var newHeight = 0;
				var currHeight = parseInt($("#modal_large > #modal_body > #mlbody").height());
				//var currWidth = parseInt($("#modal_large > #modal_body > #mlbody").width());
				
				$("#sliderprev").animate( { opacity : 0 }, 100);
				$("#slidernext").animate( { opacity : 0 }, 100);
				
				if (options.media == 'clear'){
					$("#modal_large").css({ 'height' : '908px' });
					$("#modal_large").css({ 'height' : '848px' });
					iWidth = 652;
					iHeight = 848;
				}
				
				$("#modal_large > #modal_body > #mlbody").animate( { opacity : 0 }, 400, function(){
					var html = '';
					html += '<div id="modal_large_mask" class="zero_opac">';
					html += '<span id="sliderclose"></span>';
					html += '<img src="'+director.webpath+'images/modal_large/'+idBits[0]+'/'+largeFile+'" width="'+iWidth+'" height="'+iHeight+'" alt="The Bridge" />';
					html += '</div>';
					
					$("#modal_large").append(html);
					$("#sliderclose").hover(
						function(){ $(this).css( { backgroundPosition : '-35px 0' } ); },
						function(){ $(this).css( { backgroundPosition : '0 0' } ); }
					).click(function(){ imodalClose(); });
					
					newHeight = parseInt($("#modal_large_mask").height())+55;
					var containerTop = parseInt($("#simplemodal-container").offset().top) - (newHeight - currHeight);
					
					$("#simplemodal-container").animate({ top : '-=210px' }, 400, function(){
						$("#modal_large > #modal_body").animate({ height : newHeight+'px' }, 400, function(){
							$("#modal_large_mask").animate( { opacity : 1 }, 400);
						});
					});
				});
			};

			// Initialize
			var timeout;
			if(options.auto){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};		
			
			if(options.numeric) setCurrent(0);
		
			if(!options.continuous && options.controlsFade){					
				$("a","#"+options.prevId).hide();
				$("a","#"+options.firstId).hide();				
			};
		});
		
		// Expose a function to allow the closing of the large view window
		function imodalClose(){
			$("#modal_large_mask").animate( { opacity : 0 }, 400, function(){
				$("#modal_large_mask").remove();
				
				$("#simplemodal-container").animate({ top : '+=210px' }, 400, function(){
					$("#modal_large > #modal_body").animate({ height : '428px' }, 400, function(){
						$("#modal_large > #modal_body > #mlbody").animate( { opacity : 1 }, 400, function(){
							$("#sliderprev").animate( { opacity : 1 }, 100);
							$("#slidernext").animate( { opacity : 1 }, 100);	
						});
					});
				});
			});
		};
	};
})(jQuery);




