jQuery(document).ready(function(){
	
	if ( (fb.ieVersion > 0) && (fb.ieVersion < 9) ) {
		fb.start( '#not_supported' );
	}
	
	jQuery('body #widgetshoppingcart h3').html('Shopping Basket'); // change cart to basket 
	
	jQuery('body.page-id-29, body.page-id-5 input#shippingSameBilling').change(function(){
	     parent.fb.resize();
		});
	
	if(jQuery.isFunction(jQuery.fn.sprite)) {
		var newdrop ='';
		var oldDrop = jQuery('#drop');			
		
		jQuery('#tailtip').sprite({fps: 8, no_of_frames: 3}).spStop(true);
		
		jQuery('#products, #area_line').mouseover(function() {
			jQuery('#tailtip').spStart();
		});
		jQuery('#products, #area_line').mouseout(function() {
			jQuery('#tailtip').spStop(true)
		});
	
		jQuery('#portfolio').mouseenter(function() {

			newDrop = oldDrop.clone().insertAfter(oldDrop);
			newDrop.css( 'top', '265px');
			newDrop.css( 'left', '680px');
			newDrop.css( 'background-position', '-102px 0px');
			newDrop.fadeIn('slow')
			newDrop.animate({ top:  "365px", left:"690px" }, 200,  function() { newDrop.css( 'background-position', '-204px 0px'); });
			newDrop.animate({ top: "536px", left:"702px" }, 400,  function() { 
					newDrop.css( 'background-position', '-306px 0px'); 
					setTimeout ("newDrop.css( 'background-position', '-408px 0px');",200);
					newDrop.fadeOut('slow', function() {
						 newDrop.css( 'background-position', '-0px 0px');
						 newDrop.remove();
				   });
			});
		});
		jQuery('#portfolio').mouseout(function() {
		//	jQuery('#drop').css( 'top', '265px');
		//	jQuery('#drop').css( 'left', '680px');
		});
	
	};
	cart_item_count = jQuery('div.shoppingcart .cart-widget-count', window.parent.document).html();
	if (cart_item_count) 
		jQuery('div#items_quantity', window.parent.document).html(cart_item_count.replace(/\D/g,''));
	jQuery("#menu-item-46 a").addClass("floatbox");
	jQuery("#menu-item-46 a").attr("data-fb-options", "autoFitHTML:true scrolling:no width:502px height:900px");
	jQuery("#menu-item-43 a:first").addClass("floatbox");
	jQuery("#menu-item-43 a:first").attr("data-fb-options", "scrolling:no width:400px height:550px");
	jQuery("#menu-item-43 ul a:first").addClass("floatbox");
	jQuery("#menu-item-43 ul a:first").attr("data-fb-options", "scrolling:no width:400px height:550px");
	
	var pos_tail = jQuery("#tailtip");
	var pos_t = pos_tail.position();
	//var offsett
	jQuery('#commissions').mouseover( function(e) { jQuery('#tailtip').css('left', (pos_t.left -1) +'px'); } );
	jQuery('#commissions').mouseout( function(e) { jQuery('#tailtip').css('left', (pos_t.left +1) +'px'); } );
	// Check if map exists
	if(jQuery('#map')) {
		// Loop through each AREA in the imagemap
		jQuery('#map area').each(function() {
			// Assigning an action to the mouseover event
			jQuery(this).mouseover(function(e) {
				var country_id = jQuery(this).attr('id').replace('area_', '');
				jQuery('#'+country_id).show();
				jQuery('#'+country_id).mouseout(function(e) {
				jQuery('#'+country_id).hide();
			});
			});
			// Assigning an action to the mouseout event
			jQuery(this).mouseout(function(e) {
				var country_id = jQuery(this).attr('id').replace('area_', '');
			});
			// Assigning an action to the click event
			jQuery(this).click(function(e) {
				var country_id = jQuery(this).attr('id').replace('area_', '');
				jQuery('#'+country_id).mouseout(function(ev) {
					jQuery('#'+country_id).show();
			}); 
				jQuery('#'+country_id+' a').trigger('click');
			});
		
		});
	}
	
	jQuery('#access ul li').mouseover(function(){ 
   		var submenu = jQuery(this).find('ul.sub-menu');
   		submenu.css("top", (-submenu.height()+1));  // pop up menu
   		submenu = jQuery(this).find('ul ul.sub-menu');
   		submenu.css("top", (-submenu.height()+28));  // pop up menu
   		submenu = jQuery(this).find('ul ul ul.sub-menu');
   		submenu.css("top", (-submenu.height()+28));  // pop up menu
	})	
	
	jQuery('#s_basket a').hover(function(){
        jQuery('div#items_cover').css('backgroundPosition', '-3px -3px'); 
		jQuery('div#items_quantity').css('top', '-5px');
		jQuery('div#items_quantity').css('left', '-3px');
    }, function(){
        jQuery('div#items_cover').css('backgroundPosition', '');
		jQuery('div#items_quantity').css('top', '-2px');
		jQuery('div#items_quantity').css('left', '');
    });
	
	//-------- BEGIN Carousal code -------------------	  

 	jQuery.fx.off; //switch off all FX for improved performace
  
	jQuery('#mycarousel').jcarousel({
		visible: 4.2,
		gap: '',
       	buttonNextHTML: null,
		buttonPrevHTML: null,
		animation: 0,
		itemFallbackDimension: 178
	 }); 
	 // autoscrolling if the user moves with the cursor over the clip.
	var id = 'mycarousel';
	var tsMargin = 10;
	var scrollEasing = 800;
	var scrollEasingType = 'easeOutCirc';
	var thumbnailOpacity= 0.8;
	var thumbnailFadeSpeed = 300;

	//caching vars
	var $outer_container=jQuery("#"+id+"_container"); //#(id)_container
	var $thumbScroller=jQuery("#"+id+"_container .jcarousel-container-horizontal"); 
	var $thumbScroller_container=jQuery("#"+id+"_container ul#"+id); 
	var $thumbScroller_content=jQuery("#"+id+"_container li.jcarousel-item"); 
	var $thumbScroller_thumb=jQuery("#"+id+"_container ul#"+id+" li.jcarousel-item img"); 
	var $thumbScroller_horWrapper=jQuery("#"+id+"_container jcarousel-skin-none"); 

	$thumbScroller_container.css("marginLeft",tsMargin+"px"); //add margin
	jQuery($thumbScroller_container).css("display", "block");
	$thumbScroller_horWrapper.css("width",999999); 
	var totalWidth=$thumbScroller_container.outerWidth(true)-60;  //less the gaps?
	$thumbScroller_horWrapper.css("width",totalWidth);

	var $the_outer_container= jQuery("#"+id)[0]; 
	var $placement=findPos($the_outer_container);
	var $content_mid = jQuery($thumbScroller_content).width()/2;
	var $container_mid =  jQuery($thumbScroller).width()/2;

	 if ( jQuery($thumbScroller).offset()) {
	  $container_mid += jQuery($thumbScroller).offset().left;
	 }
	$thumbScroller_content.each(function (i) {
		$curr_pos= jQuery($thumbScroller_content[rel=i]).offset().left + $content_mid;
		$new_pos = Math.cos((($container_mid - $curr_pos)/360)-0.1);
		jQuery($thumbScroller_content[rel=i]).css({"margin-top": Math.round($new_pos*100)});
    });
	
	
	function doTop_Rotate() {
	
		$thumbScroller_content.each(function (i) {
		$curr_pos= jQuery($thumbScroller_content[rel=i]).offset().left + $content_mid;
		$new_pos = Math.cos((($container_mid - $curr_pos)/360)-0.1);
		jQuery($thumbScroller_content[rel=i]).css("margin-top", Math.round($new_pos*100));
		$rotation= Math.round(($container_mid - $curr_pos)/36);
			
		if ($rotation > 26 ) {
			jQuery($thumbScroller_content[rel=i]).css("margin-top", jQuery($thumbScroller_content[rel=i]).css("margin-top")-178);
			 } else
			jQuery($thumbScroller_content[rel=i]).rotate(Math.round($rotation));
		});
		//force the page to redraw for IE by add and removing a nonexistant class, GRRRRR!!!!!!
		jQuery('body').addClass('js').removeClass('js');					 
	}

	jQuery($thumbScroller_container).css({'position':'relative','left':'960px'}).animate({left: 0}, {duration: 2000,  
				step: function( now, fx ){
					doTop_Rotate(); 

			}
	});
			
				
	function MouseMove(e){
		
			var mouseCoords=(e.pageX - $placement[1]);  
			var mousePercentX=mouseCoords/$outer_container.width(); if(mousePercentX>1){mousePercentX=1;}
			var destX=Math.round(-((totalWidth-$outer_container.width())*(mousePercentX)));
			
	//	if(totalWidth>$outer_container.width()){ //check if content needs scrolling
			$thumbScroller_container.stop(true,false).animate({left: destX}, {
				//$scroller.stop(true,false).animate({left:destX,top:destY},options.scrollEasingAmount,options.scrollEasing); 
					step: function( now, fx ){
     						doTop_Rotate();
					}
				});
	//	}
			
	}
	
	$outer_container.bind("mousemove", function(event){
	// $thumbScroller_content.bind("mousemove", function(event){
		MouseMove(event);							  
	});
	
	//function to find element Position
	function findPos(obj) {
		var curleft = curtop = 0;
		if (obj) {
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		}
		return [curtop, curleft];
	}
	
	jQuery(window).bind("resize ready", function(event) {
	$placement=findPos($the_outer_container);
		$container_mid =  jQuery($thumbScroller).width()/2;
		if ( jQuery($thumbScroller).offset()) {
		  $container_mid += jQuery($thumbScroller).offset().left;
	 	}
		doTop_Rotate();
	});
	
	//-------- END Carousal code -------------------	 
});



function showForm(href,div_id, width, height) {
	var iframe = document.createElement('iframe'),
	style = iframe.style;				
	style.position='absolute';				
	style.background='none';	
	iframe.frameBorder = 0;
	style.left = '60px';
	style.top = '10px';
	iframe.className = 'home_top_iframe';
	iframe.scrolling ='no';
	iframe.width = width; //fb.fbContent.width-20;
	iframe.height = height; //fb.fbContent.height-20;
	iframe.src = href;
	fb.fbContentWrapper.appendChild(iframe);	

	jQuery('#close_fbox').css({ "position": "absolute", "z-index": "90010", "right": "6px", "top": "6px" }).show();
	jQuery('#'+div_id).show();	

}


function hideForm() {
	fb.fbContentWrapper.getElementsByTagName('iframe')[0].style.display = 'none';
}
function showDiv(div_id, cur_div_id) {
	jQuery('#'+cur_div_id).show();	
	var iframe = document.createElement('iframe'),
	style = iframe.style;				
	style.position='absolute';				
	style.padding='140px 0 0 120px';
	iframe.frameBorder = style.left = style.top = '0';
	iframe.scrolling = 'auto';
	iframe.width = '100%'; //fb.fbContent.width;
	iframe.height = '100%'; //fb.fbContent.height;
	style.left ='-120px';
	style.top ='-50px';
	fb.fbContentWrapper.appendChild(iframe);
	jQuery(iframe).contents().find('body').html(jQuery(div_id).html());
	jQuery(iframe).contents().find('body #widgetshoppingcart h3').html('Shopping Basket');
	jQuery(iframe).contents().find('body a').css({'color': 'black', 'text-decoration' : 'none' });
	jQuery(iframe).contents().find('body #widgetshoppingcart').css({'background-color': 'rgba(255, 255, 255, 0.92)', 'width': 'auto', 'padding' : '10px','color': 'black', 'font-size' : '14px', 'font-family': 'interstate_light, InterstateLight, InterstateCondensed, InterstateLightCondensed, InterstateCondBlack, InterstateCond, InterstateBlack, Arial, Helvetica, sans-serif' });
	jQuery(iframe).contents().find('body #widgetshoppingcart td.cart-widget-remove').css({'display': 'none' });
	jQuery(iframe).contents().find('body #widgetshoppingcart td#cart-widget-links').css({'display': 'none' });
	
}
			
function loadFloatboxHtml(href) {
	fb.start(href, 'width:450 autoFitHTML:true  scrolling:no  sameBox:true ');
  	return true;
}
function loadFloatboxProductHtml(href) {
	fb.start(href, 'width:450 autoFitHTML:true  scrolling:no  sameBox:true ');
  	return true;
}
function myFunc2(href) {
	fb.reload ('<?php echo  wpsc_the_product_image(); ?>','autoFitHTML:false width:400px height:700px');
 	return true;
}
function myHideFunc(map) {
	jQuery('#'+map).hide();
  	return true;
}				
		
function myHideClose() {
	jQuery('#close_fbox').hide();
  	return true;
}			
