// OG Slider PlugIn, September 2010 //
// Omega Graphic - Antalya / omegagraphic.com //
// Author: Ozgur Gurckaya / gurckaya.com // 

(function($) {
	$.og_slider = {version: '1.0'};
	$.fn.og_slider = function(variables){
		
		variables = $.extend({}, {
			
			gallery: 0,
			speed: 200,
			duration: '',
			count: '',
			object: '',
			source: '',
			starter: '',
			currentSlide: '',
			firstActionControlFlag: false,
			previousSlide: '',
			slideWidth: '',
			slideHeight: '',
			descriptionX: 0,
			descriptionY: 0,
			fontSize: '',
			fontColor: '#fff',
			objectClass: '',
			siteRoot: ''
						
		}, variables);

		
		initialize = function() {
			
			var e = $(variables.object);
			
			$(e).css({'overflow':'hidden', 'width':variables.slideWidth + 'px', 'height':variables.slideHeight + 'px', 'padding':'0px'});
			
			$.post('og_slider.asp', {galleryID: '' + variables.gallery + ''}, function(data) {
				
				var pathArray = new Array();
				var feedback = data;
				
				pathArray = feedback.split("||");
				
				variables.count = pathArray.length
				var i = 0;
				
				while(i < variables.count - 1) {				
					
					$(e).append('<li class="subelement ' + i + '_"><img src="' + variables.siteRoot + 'images/galleries/' + variables.gallery + '/larges/' + pathArray[i] + '" desc="' + variables.objectClass + '"/></li>')
					$(e).append('<div class="subelement desc_' + i + '">' + pathArray[(i + 1)] + '</div>');
		
					i = i + 2;	
										
				}
		
				$('.subelement').css({display:'none', opacity:0, 'position':'absolute', 'z-index':'500', 'list-style-type':'none', 'font-family':'Arial, Helvetica, sans-serif','font-size':variables.fontSize + 'px','color':variables.fontColor});
				
				$('.mainPicture').css({'position':'absolute'});
	
				variables.currentSlide = variables.starter;
				
				if (variables.count > 2) {
									
					startSlide();
					
				} else {
					
				$('.subelement img').each(function() {
					
					var element = $(this);
					
					if ($(this).attr("desc") == "mainPicture") {
				
						totSize = 1800 / 413;
						var _loc2 = $(window).width() / ($(window).height() - 468);
						
						if ($(window).width() > 1800) {
							
							$(this).css({'width':$(window).width() + 'px'});
							$(this).css({'height':Math.round($(window).width() / totSize) + 'px'});		
							
							width = $(window).width();
							
							$('.subelement').css({'left':'0px'});													
								
						} else {
							
							width = 1800;
							$('.subelement').css({'left':0 - ((width - $(window).width()) / 2) + 'px'});
							
						}
						
						$('.subelement').css({'top':'10px'});
						
					}
				
				});					
				
				$('.subelement.' + variables.currentSlide + '_').css({'display':''}).animate({opacity:1}, variables.speed, "linear", function() {
			
					$('.subelement.desc_' + variables.currentSlide).css({'display':'', top:variables.descriptionY + 'px', left:variables.descriptionX + 'px'}).animate({opacity:1}, Math.round(variables.speed / 2), "linear");
				 
				});	
				
												
					
				}
					
				
			});
			
			
		}
		
		startSlide = function() {		
			
			var durat = !variables.firstActionControlFlag ? (1):(variables.duration);
			
			$('.subelement img').each(function() {
				
				var element = $(this);
				
				if ($(this).attr("desc") == "mainPicture") {
			
					totSize = 1800 / 413;
					var _loc2 = $(window).width() / ($(window).height() - 468);
					
					if ($(window).width() > 1800) {
						
						$(this).css({'width':$(window).width() + 'px'});
						$(this).css({'height':Math.round($(window).width() / totSize) + 'px'});		
						
						width = $(window).width();
						
						$('.subelement').css({'left':'0px'});													
							
					} else {
						
						width = 1800;
						$('.subelement').css({'left':0 - ((width - $(window).width()) / 2) + 'px'});
						
					}
					
					$('.subelement').css({'top':'10px'});
					
				}
			
			});		

			if(variables.firstActionControlFlag) {
				
				$('.subelement.desc_' + variables.previousSlide).animate({opacity:0}, Math.round(variables.speed * 2), "linear");			
				
				$('.subelement.' + variables.previousSlide + '_').animate({opacity:0}, variables.speed, "linear");
			}
			
			variables.firstActionControlFlag = true;						
			
			$('.subelement.' + variables.currentSlide + '_').delay(durat).css({'display':''}).animate({opacity:1}, variables.speed, "linear", function() {
			
				$('.subelement.desc_' + variables.currentSlide).css({'display':'', top:variables.descriptionY + 'px', left:variables.descriptionX + 'px'}).animate({opacity:1}, Math.round(variables.speed / 2), "linear");
					
				variables.previousSlide = variables.currentSlide > 0 ? (variables.currentSlide - 2) : (variables.count - 2);
				variables.currentSlide = variables.currentSlide < variables.count - 2 ? (variables.currentSlide + 2) : (0);

				startSlide();
					
				 
			});
			
		}

		initialize();
	}
	
})(jQuery);

