
jQuery(document).ready(function() {

	//cycle plugin effect
	//some issues with the timer.. delay is not properly working so set a bigger number
	$('.slide').cycle({
	timeout:10000}
	);

	//carousel effect
	$("#piclist").jcarousel({
		scroll:1
	});

	//galleria for display
	$('#piclist').galleria({
		history   : false, // activates the history object for bookmarking, back-button etc.
		clickNext : false, // helper for making the image clickable
		insert    : '#mainimg', // the containing selector for our main image
		onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes

			// fade in the image & caption
			if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
				image.css('display','none').fadeIn(1000);
			}
			caption.css('display','none').fadeIn(1000);

			// fetch the thumbnail container
			var _li = thumb.parents('li');

			// fade out inactive thumbnail
			_li.siblings().children('img.selected').fadeTo(500,0.3);

			// fade in active thumbnail
			thumb.fadeTo('fast',1).addClass('selected');

			// add a title for the clickable image
			image.attr('title','');
			
			//image changing the source.
			var theimage = $('.galleria_wrapper img').get(0);
			var str = theimage.src;
			//alert(str);
			var dot = '.';
			str = str.replace('_thumb.',dot);
			str = str.replace(/\/thumb\//,'/products/');
			
			theimage2 = jQuery("<img>").attr('src',str);
			
			//alert($(theimage2).attr('height'));
			//alert($(theimage2).attr('src'));	
			//swap file to prevent flickering
			$('.galleria_wrapper').html(theimage2);
			var widthsz = $('[name="width"]').val();
			var heightsz = $('[name="height"]').val();

			//JQzoom specific, add a css class, modify the alt text
			//image.attr('alt',image.attr('src'));
			//image.addClass('jqzoom');

			//image resizing
			var max_width = parseInt($('div.galleria_wrapper').css('width')); 	//Sets the max width, in pixels, for every image
			var max_height = parseInt($('div.galleria_wrapper').css('height'));
			var selector = 'div.galleria_wrapper img'; 	//Sets the syntax for finding the images.  Defaults to all images.
			//For images inside of a particular element (id="abc") or class (class="abc"),
			//use '.abc img' or '#abc img' respectively.  Don't leave off the img tag!!!
			//End configuration options.  You don't need to change anything below here.

			$(theimage2).each(function(){
				var width = widthsz;
				var height = heightsz;
				var new_width =0;
				var new_height = 0;
				//retrieve image src
				var default_image = $(this).attr("src");
				//retrieve image src length
				var length = $(this).attr("src").length;
				//check if default_img.jpg, ignore resize.
				
				if(default_image.substring((length-16), length) == "/default_img.jpg")
				{
					height = $(this).height();					
					var padding = max_height - height;
					//divide to two, top and bottom 
					var padding = padding/2;
					$('div.galleria_wrapper').css('padding-top',padding+'px');
					$('div.galleria_wrapper').css('height',height+'px');				

				}
				
				else{				
				//scale height
				if (height > max_height) {
					//set variables for manipulation
					var ratio = (width / height);
					var newer_height = max_height;
					var newer_width = (newer_height * ratio);
					$(this).height(newer_height).width(newer_width);
				}
				

				if (width > max_width) {
					//Set variables	for manipulation
					var ratio = (height / width );
					new_width = max_width;
					new_height = (new_width * ratio);
					//Shrink the image and add link to full-sized image
					$(this).height(new_height).width(new_width);

					if (new_height > max_height) {
						//set variables for manipulation
						var ratio = (new_width / new_height);
						var newer_height = max_height;
						var newer_width = (newer_height * ratio);
						$(this).height(newer_height).width(newer_width);
					}

				} //ends if statement

				
				if (new_height < max_height && new_height != 0){
					var padding = max_height - new_height;
					var padding = padding/2;	
					$('div.galleria_wrapper').css('padding-top',padding+'px');
					$('div.galleria_wrapper').css('height',new_height+'px');
					
				}
				else if (height < max_height && new_height == 0){
					var padding = max_height - height;					
					var padding = padding/2;	
					$('div.galleria_wrapper').css('padding-top',padding+'px');
					$('div.galleria_wrapper').css('height',height+'px');
					
				}
				}
				//for thickbox
				$(this).wrap('<a href="' + theimage2.attr('src')+'" class="thickbox"></a>');
				tb_init('a.thickbox');
			} //ends each function
			);

		},
		onThumb : function(thumb) { // thumbnail effects goes here

			// fetch the thumbnail container
			var _li = thumb.parents('li');

			// if thumbnail is active, fade all the way.
			var _fadeTo = _li.is('.active') ? '1' : '0.3';

			// fade in the thumbnail when finnished loading
			thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);

			// hover effects
			thumb.hover(
			function() { thumb.fadeTo('fast',1); },
			function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
			)
		}
	});

	//navigation fading effect
	$('ul.sf-menu').superfish();

	var $news = $('#ticker1');//we'll re use it a lot, so better save it to a var.

	//function to run ticker
	function runtick(obje)
	{
		//jquery ticker
		obje.serialScroll({
			items:'div',
			duration:4000,
			force:true,
			axis:'y',
			easing:'linear',
			lazy:true,// NOTE: it's set to true, meaning you can add/remove/reorder items and the changes are taken into account.
			interval: 1,
			step:-1
		});

		//bind hovering effect for that object
		$(obje).hover(
		function(){
			$(this).trigger('stop');
			$(this).stop();
		},function(){
			$(this).trigger('start');
		});
	}

	//run ticker
	//$($news).each();
	//runtick($news);

});





