$(document).ready(function(){

	// ie only css rounded corners markup
	//if ($.browser.msie) {

		if($('#inner #main #main-center-panel div.col-2 div.panel-blank, #inner #main #main-center-panel div.col-2 div.panel, #inner #main #main-center-panel div.col-2  div.no-border, #inner #main #main-center-panel div.rounded').length > 0 ){
			$('#inner #main #main-center-panel div.col-2 div.panel-blank, #inner #main #main-center-panel div.col-2 div.panel, #inner #main #main-center-panel div.col-2  div.no-border, #inner #main #main-center-panel div.rounded').each(function(){
				$(this).prepend('<!--[if IE ]><div class="top"></div><![endif]-->').append('<!--[if IE ]><div class="bottom"></div><![endif]-->');
			});
		}
	//}
		
	// home page only
	if(jQuery.fn.showhide){
		if($('#banner a.showhide').length > 0 ){
			$('#banner a.showhide').showhide({
				animate:true,
				event:'click',
				autoScroll: true,
				autoScrollTimeout:5000
			});
		}
	}
	
	if(jQuery.fn.jcarousel){
		
		if($('#latest-news ul').length > 0 ){ // check if element is on page
			$('#latest-news ul').jcarousel({
				buttonNextHTML : '<a class="next">next</a>',
				buttonPrevHTML : '<a class="prev">prev</a>',
				scroll: 1,
				vertical: true,
				wrap : 'circular'
			});
		}

	
		if($('#carousel').length > 0 ){
			$('#carousel').jcarousel({
				buttonPrevHTML : '<a class="prev">prev</a>',
				buttonNextHTML : '<a class="next">next</a>',
				scroll : 1,
				visible: 6,
				wrap : 'circular',
				auto: 1

			});
		}		
		function onBefore(carousel, item, idx, state) { 
			
			if($(item).children('.overlay').length == 0){
				var overlay = $('<div class="overlay" style="z-index:2;"><p>'+$(item).children('img').attr('alt')+'</p></div>');
				
				$(item).append(overlay);
				overlay.animate({bottom:18}, {easing: 'easeInCubic'},{duration:100});
			}
		} 
		function onAfter(carousel, item, idx, state) { 
			
			if($(item).children('.overlay').length > 0){
				$(item).children('.overlay').remove();
			}
			
		}
		
		if($('#gallery ul').length > 0 ){ // check if element is on page
			$('#gallery ul').jcarousel({
				buttonNextHTML : '<a class="gallery-next">next</a>',
				buttonPrevHTML : '<a class="gallery-prev">prev</a>',
				scroll: 1,
				visible: 3,
				vertical: true,
				wrap : 'circular',
				auto: 5,
				itemFirstInCallback:onBefore,
				itemFirstOutCallback:onAfter/*
				itemVisibleInCallback: {
				  onBeforeAnimation: onBefore,
				  onAfterAnimation: onAfter
				}*/
			});
		}
	}
	
	// our work page only	
	if($('#main #main-center-panel ul.listing li div.background').length > 0){ // check if element is on page
		$('#main #main-center-panel ul.listing li').each(function(){
			

            var time = 250;
            var hideDelay = 250;
 			
            var hideDelayTimer = null;
 
            var beingShown = false;
            var shown = false;
			
			var details = $(this).children('div.background');
			
			if($(this).hasClass('nofade') == false){				
				
				//handle parent / child events
				$(this).mouseover(function(){
					
					$(this).addClass('active');
					if (hideDelayTimer) clearTimeout(hideDelayTimer);					   
					if (beingShown || shown) {
						// don't trigger the animation again
						return;
					} else {
						 // reset position of info box
                   		beingShown = true;
					
						// reset position of info box based on sttyle sheet values
						details.css({
							display: 'block'
						}).stop().animate({opacity: 0.2, 'filter': ''}, 50, 'swing', function() {
							beingShown = false;
							shown = true;
							$(this).css({'background-image':'url("/css/skins/default/img/content/home-row1-listing-ul-li-on-bg.png")'});
							$(this).animate({opacity: 1, 'filter': ''}, 200, 'swing');
						});
					}
					
					return false;
					
				}).mouseout(function(){					
					
					if (hideDelayTimer) clearTimeout(hideDelayTimer);
					hideDelayTimer = setTimeout(function () {
				   		hideDelayTimer = null;
						details.stop().animate({opacity: 0.2, 'filter': ''}, time, 'swing', function () {
							shown = false;
							$(this).css({'background-image':'url("/css/skins/default/img/content/home-row1-listing-ul-li-bg.png")'});
							$(this).animate({opacity: 1, 'filter': ''}, 500, 'swing');
						});
					}, hideDelay);
					
					return false;
				});
				
			}
		});
	}
	
	/* iviewer - image zoomer */
	
	//SHOW IVIEWER - hidden by default
	if (jQuery.fn.iviewer)
	{
		//HIDE MAIN IMAGE - visible by default
		$("#main-image").css("display", "none"); 
	
		if ($('.iviewer').length > 0 ){
			$(".iviewer").css("display", "block"); 	

			var map_image = $("#map_image").attr("src");

			var iviewer = {};
			$("#iviewer").iviewer(
				{
				zoom:25,
				src: map_image, 
				update_on_resize: false,
				initCallback: function ()
				{
				   var object = this;
				   $("#in").click(function(){ object.zoom_by(1);}); 
				   $("#out").click(function(){ object.zoom_by(-1);}); 
				   $("#fit").click(function(){ object.fit();}); 
				   $("#orig").click(function(){  object.set_zoom(70); }); 
				   $("#update").click(function(){ object.update_container_info();});
				},
				onMouseMove: function(object, coords) { },
				//onStartDrag: function(object, coords) { return false; }, //this image will not be dragged
				onDrag: function(object, coords) { }
			});
		}	  
	}
	
	/* our shops page only */
	
	//apply accordian behaviour to ul li structure
	if($('.accordion').length > 0 ){ // check if element is on page
	
		function SlideDown(e){
			$(e).children('div.content').show();
			
			// changed speed of animation for ie6 			
			if($.browser.msie && $.browser.version=="6.0"){
				var duration = 0;
			}else{
				var duration = 500;
			}
			
			$(e).stop().animate(
				{ height: 267 }, // what we are animating
				{
					duration: duration, // how fast we are animating
					easing: 'easeInCubic', // the type of easing
					complete: function() { // the callback
						// Animation complete.
						$(this).addClass('selected');
						// Show corresponding icon on floorplan
						highLightMarker($(this));
						
						// look for child a external link adn open in new window
						if($(this).find('a').length > 0){
						
							// pass link to var
							var link = $(this).find('.content a');
							
							// unbind previous click event to stop continual binding
							link.unbind('click');
							
							// add title telling user what will happen when clicked
							link.attr('title', "Open link in a new window.");
							
							// bind click event
							link.bind('click', function(){							
								window.open($(this).attr('href'),'_blank');
							});
						}
			
				   	}
			});
			
			// hide all others
			SlideUp($(e).siblings('li.selected'));
			
		}
		
		function highLightMarker(e){
						
			// look up item on map and set to selected
			$('ul.floorplan-markers a').removeClass('selected');
			
			marker_id = $(e).attr('id').substring($(e).attr('id').indexOf("-"));
			marker_id = '#marker'+marker_id;
			
			$(marker_id).addClass('selected');
		}
		
		function SlideUp(e){
			
			// changed speed of animation for ie6 	
			if($.browser.msie && $.browser.version=="6.0"){
				var duration = 0;
			}else{
				var duration = 500;
			}
			
			$(e).stop().animate(
				{ height: 16 }, // what we are animating
				{
					duration: duration, // how fast we are animating
					easing: 'easeInCubic', // the type of easing
					complete: function() { // the callback
						// Animation complete
						$(this).removeClass('selected');
						$(e).children('div.content').hide();

				   	}
			});	
		}

		$i = 1;
		
		$('ol.accordion li').each(function(){
			
			
			if($(this).hasClass('first') == true){
				$i = 1;
			}
			
			$(this).data('height', $(this).height());
				
			// bind click behaviour to contact link
			$(this).bind('click', function(){
				
				if($(this).hasClass('selected') == false){
					// slide down
					SlideDown($(this));
				}
				
				return false;
			});
			
			//hide all content boxes by default
			$(this).children('div.content').hide();
			
			// pop a number before each li item
			span = $('<span />').html($i+'.');
			
			$(this).prepend(span);

			$i ++;
            
		});
	}
	
	if($('ul.floorplan-markers').length > 0 ){ // check if element is on page
		
		$i = 1;
		
		$('ul.floorplan-markers li a').each(function(){
				
			var target_id = null;
			
			//if the first occurance of the # in attr('href') is greater than zero then it is due to IE bug
			if($(this).attr('href').indexOf("#") > 0){
				//bug fix for IE miss hadnling of attr('href')
				target_id = $($(this).attr('href').substring($(this).attr('href').indexOf("#")));				
			}else if($(this).attr('href').indexOf("#") == 0){
				target_id = $($(this).attr('href'));
			}
			
			if(target_id.length > 0){
				$(this).bind('click',function(){
				
					
					// place state class on clicked link
					if($(this).hasClass('selected') == false){
						
						// trigger click event on target_id
						target_id.trigger('click');
					
						$(this).addClass('selected');
					}			
					
					// remove other existing active class states
					$('ul.floorplan-markers li a').not(this).removeClass('selected');
					
					return false;
				});
			}
			
			//by default show the first item
			/*if($i == 1){
				$(this).addClass('selected');
			}*/
			
			$i ++;
	        
		});
		
		function runFromHash(){
		
			$('ul.floorplan-markers li a').each(function(){
				
				var target_id = null;
				
				//if the first occurance of the # in attr('href') is greater than zero then it is due to IE bug
				if($(this).attr('href').indexOf("#") > 0){
					//bug fix for IE miss hadnling of attr('href')
					target_id = $($(this).attr('href').substring($(this).attr('href').indexOf("#")));
					target_href = $(this).attr('href').substring($(this).attr('href').indexOf("#"));						
				}else if($(this).attr('href').indexOf("#") == 0){
					target_id = $($(this).attr('href'));
					target_href = $(this).attr('href');
				}
				
				if(location.hash == target_href){
				
					if(target_id.length > 0){
						
						// trigger click event on target_id
						target_id.trigger('click');
						
						// place state class on clicked link
						if($(this).hasClass('selected')){
							$(this).removeClass('selected')
						}else{
							$(this).addClass('selected');
						}					
						
						/* get the parent of the link and show it */
						parent =  $(this).closest('div.floorplan');						
						$('div.floorplan').hide();	// hide other sibling parents	
						parent.show();
						
						/* get the parent of the target and show it */
						parent =  target_id.closest('div.panel');
						$('div.panel').hide();		// hide other sibling parents		
						parent.show();
						
						// remove other existing active class states
						$('ul.floorplan-markers li a').not(this).removeClass('selected');
						
					}
				
				}
		        
			});
			
			return false;
		}
		
		$(window).hashchange(function() {
			if(typeof(location.hash) != 'undefined'){
				runFromHash();
			}
		});
		
		if(typeof(location.hash) != 'undefined'){
			runFromHash();
		}
	}
	
	if(jQuery.fn.showhide){
		if($('div.our-shops a.showhide').length > 0 ){
			$('div.our-shops a.showhide').showhide({
				animate:true,
				event:'click',
				autoScroll: true,
				autoScrollTimeout:5000
			});
		}
	}

});
