// Sets a delay. This function is a "jquery plugin", and is chainable
(function($) {
	$.fn.idle = function(time) {
		return this.each(function() {
			var el = $(this);
			el.queue(function() { 
				setTimeout(function() {
					el.dequeue(); 
				}, time);
			});
		});
	}
})(jQuery);

(function($) {
	$.fn.holdQueue = function(time, type) {
	  time = time || 1000;
	  type = type || "fx";
	  return this.queue(type, function() {
		var self = this;
		setTimeout(function() {
		  $(self).dequeue();
		}, time);
	  });
	}
})(jQuery);

$(document).ready( function() {
	
	Global.init();
	
	Home.init();
	
	switchThumb();
	
});

Global = {
	init: function() {
		cleanRounded();
		
		if( $('.bluGallery').parent().css('text-align') == 'center' ) {
			$('.bluGallery img:first').css('margin-left','0');
			$('.bluGallery img:last').css('margin-right','0');
		}
		$("a[rel^='prettyPhoto']").prettyPhoto({
			animationSpeed: 'normal', /* fast/slow/normal */
			opacity: 0.60, /* Value between 0 and 1 */
			showTitle: false, /* true/false */
			allowresize: true, /* true/false */
			default_width: 800,
			default_height: 344,
			counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
			theme: 'facebook', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
			hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
			wmode: 'opaque', /* Set the flash wmode attribute */
			autoplay: true, /* Automatically start videos: True/False */
			modal: false, /* If set to true, only the close button will close the window */
			changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
			callback: function(){}, /* Called when prettyPhoto is closed */
			markup: '<div class="pp_pic_holder"> \
						<div class="pp_top"> \
							<div class="pp_left"></div> \
							<div class="pp_middle"></div> \
							<div class="pp_right"></div> \
						</div> \
						<div class="pp_content_container"> \
							<div class="pp_left"> \
							<div class="pp_right"> \
								<div class="pp_content"> \
									<div class="pp_fade"> \
										<a href="#" class="pp_expand" title="Expand the image">Expand</a> \
										<div class="pp_loaderIcon"></div> \
										<div class="pp_hoverContainer"> \
											<a class="pp_next" href="#">next</a> \
											<a class="pp_previous" href="#">previous</a> \
										</div> \
										<div id="pp_full_res"></div> \
										<div class="pp_details clearfix"> \
											<a class="pp_close" href="#">Close</a> \
											<h2 class="pp_description"></h2> \
											<div class="pp_nav"> \
												<a href="#" class="pp_arrow_previous">Previous</a> \
												<p class="currentTextHolder">0/0</p> \
												<a href="#" class="pp_arrow_next">Next</a> \
											</div> \
										</div> \
									</div> \
								</div> \
							</div> \
							</div> \
						</div> \
						<div class="pp_bottom"> \
							<div class="pp_left"></div> \
							<div class="pp_middle"></div> \
							<div class="pp_right"></div> \
						</div> \
					</div> \
					<div class="pp_overlay"></div> \
					<div class="ppt"></div>',
			image_markup: '<img id="fullResImage" src="" />',
			flash_markup: '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{width}" height="{height}"><param name="wmode" value="{wmode}" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="{path}" /><embed src="{path}" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="{width}" height="{height}" wmode="{wmode}"></embed></object>',
			quicktime_markup: '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" height="{height}" width="{width}"><param name="src" value="{path}"><param name="autoplay" value="{autoplay}"><param name="type" value="video/quicktime"><embed src="{path}" height="{height}" width="{width}" autoplay="{autoplay}" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/"></embed></object>',
			iframe_markup: '<iframe src ="{path}" width="{width}" height="{height}" frameborder="no"></iframe>',
			inline_markup: '<div class="pp_inline clearfix">{content}</div>'
		});
	}
}

Home = {

	arr: new Array(),
	navArr: new Array(),
	
	init: function() {
		var selected = window.location.hash;
		var arr1 = new Array();
		var arr2 = new Array();
		Home.arr[0] = arr1;
		Home.arr[1] = arr2;
		Home.navArr[0] = '#bannerNav';
		Home.navArr[1] = '#servicesNav';
		
		$("#main .content>div.banner").each(function() { arr1.push("#"+$(this).attr("id")); });
		$("#home #contentBL .content>div.service").each(function() { arr2.push("#"+$(this).attr("id")); });
		
		if( (selected.length) && (selected != "#") ) {
			$.each(arr,function(i, val) {
				if( jQuery.inArray(selected, val) != -1 ) {
					var nav = $(selected).parent().children('ul').attr('id');
					var current = $('#'+nav+' li.selected>a');
					var to = selected;
										
					changeSelected(current.parent(),$('#'+nav+' li a[href='+selected+']').parent());
					changeMain($(current.attr("href")),$(to));
				}
			});
		}
		
		$('#banners .autoRotate').before('<div class="cycleNav">').cycle({
			fx:     'fade', 
			speed:   600, 
			timeout: 5000,
			pager:  '#banners .cycleNav'
		});
		$('#banners .cycleNav a').each(function(i) {
			var adjustedIndex = i+1;
			$('#banners .cycleNav a').eq(i).attr('href','banner_'+adjustedIndex);
		});
		
		$('#home #contentBL .autoRotate').before('<div class="cycleNav">').cycle({
			fx:     'fade', 
			speed:   600, 
			timeout: 5000,
			pager:  '#contentBL .cycleNav'
		});		
		$('#home #contentBL .cycleNav').addClass('graphicBullets');
		$('#home #contentBL .cycleNav a').html('');
		$('#home #contentBL .cycleNav a').each(function(i) {
			var adjustedIndex = i+1;
			$('#contentBL .cycleNav a').eq(i).attr('href','services_'+adjustedIndex);
		});
		
		$('#home #contentBL .cycleNav a').eq(0).attr('id','developmentDesign');
		$('#home #contentBL .cycleNav a').eq(0).addClass('left');
		$('#home #contentBL .cycleNav a').eq(0).addClass('top');
		
		$('#home #contentBL .cycleNav a').eq(1).attr('id','mediaContentManagement');
		$('#home #contentBL .cycleNav a').eq(1).addClass('right');
		$('#home #contentBL .cycleNav a').eq(1).addClass('top');
		
		$('#home #contentBL .cycleNav a').eq(2).attr('id','eCommerce');
		$('#home #contentBL .cycleNav a').eq(2).addClass('left');
		$('#home #contentBL .cycleNav a').eq(2).addClass('bot');
		
		$('#home #contentBL .cycleNav a').eq(3).attr('id','orderFulfillment');
		$('#home #contentBL .cycleNav a').eq(3).addClass('right');
		$('#home #contentBL .cycleNav a').eq(3).addClass('bot');
	}
	
}


function switchThumb(){

	$('#rightcol ul.thumbs>li').click( function (e) {
		e.preventDefault();
		var index = ($(this).index())+1;

		$.post("/asyncControllers/loadMainImage.php", 
			{ 
				object: 'item',
				instanceId: itemId,
				position: index
			},
			function(response){
			
			if (!response.error)
			{
				$('.mainimage img').attr('src',response.imagePath);
			}
			
		}, "json");


		$(this).parent().children().each( function() {
			if( $(this).hasClass("active") ) {
				$(this).removeAttr("class");
			}			
		});
		
		$(this).addClass("active");
		
	});
	
}


function rotateContent(container,obj) {
	var current = $(container+' li.selected>a');
	var to = obj.hash;
	if(current.attr("href") != to) {
		Home.changeSelected(current.parent(),$(obj).parent());
		Home.changeMain($(current.attr("href")),$(to));
	}
}
		
function changeSelected(source,dest) {
	source.removeClass('selected');
	dest.addClass('selected');
}

function changeMain(source,dest) {
	source.fadeOut(300,function(){
		dest.fadeIn(300);
	});
}

function cleanRounded() {
	
	if( !($.browser.msie) ) {
		$(".rounded").children().each(function(){
			$(this).remove(".top");
			$(this).remove(".bottom");
		});
	}
	
}
