$(document).ready(function(){
	//PRELOAD IMAGES FUNCTION
	(function($){
		var cache = [];
		$.preLoadImages = function(){
			var args_len = arguments.length;
			for(var i = args_len; i--;){
				var cacheImage = document.createElement('img');
				cacheImage.src = arguments[i];
				cache.push(cacheImage);
			}
		}
	})(jQuery);
	//IE6 PNG FIX
	if ($.browser.msie && $.browser.version.substr(0, 1) < 7){
		DD_belatedPNG.fix('img, div, span');
	}
	//COLORBOX
	$('a[rel="colorbox"]').colorbox();
	//AUDIO PLAYER
	if($('#jquery_jplayer').length > 0){
		(function($){
			//vars
			var player_is_paused = true;
			//init player
			$('#jquery_jplayer').jPlayer({
				ready: function(){
					$(this).jPlayer('setMedia', {
						mp3: $('#jquery_jplayer').attr('title')
					}).jPlayer('pause');
				},
				ended: function (event) {
					$(this).jPlayer('play');
				},
				swfPath: 'http://artfuldodger.com/wp-content/themes/artfuldodger/js',
				supplied: 'mp3, ogg',
				solution: 'html, flash'
			});
			//play-pause controller
			$('div.jp-play-pause').click(function(){
				$('#jquery_jplayer').jPlayer(player_is_paused ? 'play' : 'pause');
				$(this).toggleClass('changed');
				player_is_paused = !player_is_paused;
				return false;
			});
			//cleanup
			delete $;
		}($));
	}
	//COLLECTION SLIDER
	if($('body.collection').length > 0){
		(function($){
			//vars
			var thumbs_per_row = 3;
			var rows_per_page = 3;
			var current_row = 0;
			var current_thumb_index = 0;
			var row_height = 259;
			var scroll_up = $('div.collection div.left div.scroll_up');
			var scroll_down = $('div.collection div.left div.scroll_down');
			var thumbs_container = $('div.collection div.thumbs');
			var thumbs_scroller = $('div.scroller', thumbs_container);
			var thumbs = $('a', thumbs_scroller);
			var full_placeholder = $('div.collection div.full');
			var model_title	 = $('div.collection div.model_title');
			//calculations
			var rows_count = Math.ceil(thumbs.length / thumbs_per_row);
			//functions
			function update_scrolls(){
				scroll_down.hide();
				scroll_up.hide();
				if(rows_count > rows_per_page){
					if(current_row <= rows_count - rows_per_page - 1){
						scroll_down.show();
					}
					if(current_row > 0){
						scroll_up.show();
					}
				}
				thumbs_scroller.stop().animate({'margin-top': -1 * current_row * row_height}, 'slow');
			}
			//update full
			function update_full(){
				var current_thumb = $(thumbs.get(current_thumb_index));
				full_placeholder.html('<img src="' + current_thumb.attr('href') + '" />');
				model_title.html('<h2>' + $('div.meta span.title', current_thumb).html() + '</h2><span>' + $('div.meta span.subtitle', current_thumb).html() + ' $' + $('div.meta span.price', current_thumb).html() + '</span>');
			}
			//full hover
			full_placeholder.hover(
				function(){
					var current_thumb = $(thumbs.get(current_thumb_index));
					if($('div.meta span.alternative_image_url', current_thumb).text() !== current_thumb.attr('href')){
					$('img', full_placeholder).stop().fadeTo('fast', 0, function(){
						$('img', full_placeholder).attr('src', $('div.meta span.alternative_image_url', current_thumb).text()).fadeTo('fast', 1);
					});
					}
				},
				function(){
					var current_thumb = $(thumbs.get(current_thumb_index));
					if($('div.meta span.alternative_image_url', current_thumb).text() !== current_thumb.attr('href')){
					$('img', full_placeholder).stop().fadeTo('fast', 0, function(){
						$('img', full_placeholder).attr('src', current_thumb.attr('href')).fadeTo('fast', 1);
					});
					}
				}
			);
			//init
			update_scrolls();
			update_full();
			$(thumbs).each(function(i, item){
				$.preLoadImages($(item).attr('href'), $('div.meta span.alternative_image_url', item).text());
			})
			//scroll actions
			scroll_up.click(function(){
				current_row--;
				update_scrolls();
			});
			scroll_down.click(function(){
				current_row++;
				update_scrolls();
			});
			//on thumb click
			$(thumbs).each(function(i, item){
				$(item).click(function(){
					current_thumb_index = i;
					update_full();
					return false;
				});
			});
			//cleanup
			delete $;
		}($));
	}
	//HOME BG ROTATOR
	if($('#page_bg').length > 0){
		(function($){
		var timeout = 10000;
			var current_img_index = 0;
			var images = $('#page_bg img');
			//functions
			function update_bg_img(){
				if(current_img_index >= images.length){
					current_img_index = -1;
					images.fadeOut('slow', function(){
						images.hide();
					});
				}
				$(images.get(current_img_index)).fadeIn('slow');
				current_img_index++;
				setTimeout(update_bg_img, timeout);
			}
			setTimeout(update_bg_img, timeout);
		}($));
	}
});
