jQuery.noConflict();

jQuery(document).ready(function(){
	// activates the lightbox page, if you are using a dark color scheme use another theme parameter
	my_lightbox("a[rel^='prettyPhoto'], a[rel^='lightbox']");
	
	k_menu(); // controls the dropdown menu
	
	
});

jQuery(window).load(function(){
	/*image slider (alternate)*/	
	if (jQuery('#featured_image img').length>0){	
		jQuery('#featured_image img').not('.current_img').css({display:"none"});
		// set the automatic image rotation, number is time between transitions in miliseconds
		interval = setInterval(function() { k_fader("#featured_image img",'1'); }, 4000); 	
	}
	jQuery('#slider').nivoSlider();
	
	jQuery(".scrollable").scrollable();

	jQuery(".items img").click(function() {
		// see if same thumb is being clicked
		if (jQuery(this).hasClass("active")) { return; }
		// calclulate large image's URL based on the thumbnail URL (flickr specific)
		var url = jQuery(this).attr("src").replace("_t", "");
		// get handle to element that wraps the image and make it semi-transparent
		var wrap = jQuery("#image_wrap").fadeTo("medium", 0.5);
		// the large image from www.flickr.com
		var img = new Image();

		// call this function after it's loaded
		img.onload = function() {
			// make wrapper fully visible
			wrap.fadeTo("fast", 1);
			// change the image
			wrap.find("img").attr("src", url);
		};
		// begin loading the image from www.flickr.com
		img.src = url;
		// activate item
		jQuery(".items img").removeClass("active");
		jQuery(this).addClass("active");
	// when page loads simulate a "click" on the first image
	});
	
	jQuery("#map").gMap({
		latitude: 45.429632,
		longitude: 8.73923,
		zoom: 16,
		controls: [],
		scrollwheel: false,
		maptype: G_NORMAL_MAP,
		markers: [{
			id: 1, 
			html: "E' GYA' sPosa di Ester Rome'<br>via Mameli, 14 - 28069 Trecate (No)<br>tel/fax +39.0321.783061<br>info@egyasposa.it", 
			latitude: 45.429632, 
			longitude: 8.73923,
			icon:{ 
				image: "img/icon.png", 
				iconsize: [26, 46], 
				iconanchor: [12,46], 
				infowindowanchor: [12, 0] }
			}		
		]
	});
	
});

function k_menu(){
	jQuery("#nav a, .subnav a").removeAttr('title');
	jQuery("#nav ul").css({display: "none"}); // Opera Fix
	jQuery("#nav li").each(function(){	
		var $sublist = jQuery(this).find('ul:first');
		jQuery(this).hover(function(){	
			$sublist.stop().css({overflow:"hidden", height:"auto", display:"none"}).slideDown(400, function(){
				jQuery(this).css({overflow:"visible", height:"auto"});
			});	
		},
		function(){	
			$sublist.stop().slideUp(400, function()	{	
				jQuery(this).css({overflow:"hidden", display:"none"});
			});
		});	
	});
}

function k_fader($items_to_fade, $next_or_prev){	
	var $items = jQuery($items_to_fade);
	var $currentitem = $items.filter(":visible");
	var $new_item;
	var $selector;
	
	$items.css('visibility','visible');
	
	if($items.length > 1){
		for(i = 0; i < $items.length; i++){
			if($items[i] == $currentitem[0]){	
				$selector = $next_or_prev >= 0 ? i != $items.length-1 ? i+1 : 0 : i == 0 ? $items.length-1 : i-1;				
				$new_item = jQuery($items[$selector]);
				break;
			}
		}
		if( $new_item.css("display") == "none" ){	
				$currentitem.css({zIndex:2});
				$new_item.css({zIndex:3}).fadeIn(1200, function(){
					$currentitem.css({display:"none"});
				});
			}
	}
}

function my_lightbox($elements){
	jQuery($elements).prettyPhoto({
		"theme": 'light_square' /* light_rounded / dark_rounded / light_square / dark_square */																
	});
	jQuery($elements).each(function(){	
		var $image = jQuery(this).contents("img");
		$newclass = 'lightbox_video';	
		if(jQuery(this).attr('href').match(/(jpg|gif|jpeg|png|tif)/)) $newclass = 'lightbox_image';
		if ($image.length > 0){	
			if(jQuery.browser.msie &&  jQuery.browser.version < 7) jQuery(this).addClass('ie6_lightbox');
			var $bg = jQuery("<span class='"+$newclass+" ie6fix'></span>").appendTo(jQuery(this));
			jQuery(this).bind('mouseenter', function(){
				$height = $image.height();
				$width = $image.width();
				$pos =  $image.position();		
				$bg.css({height:$height, width:$width, top:$pos.top, left:$pos.left});
			});
		}
	});	
	
	jQuery($elements).contents("img").hover(function(){
		jQuery(this).stop().animate({opacity:0.5},400);
	},
	function(){
		jQuery(this).stop().animate({opacity:1},400);
	});
}
