// JavaScript Document
/* VIDEO */
$(window).load(function(){
	var vid_w = 670;
	var vid_h = 377;
	$(".youtubevideo").each(function(){
		var code = $(".code", this).html();
		$(this).html('<object width="'+vid_w+'" height="'+vid_h+'"><param name="movie" value="http://www.youtube.com/v/'+code+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+code+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+vid_w+'" height="'+vid_h+'"></embed></object>');
	})
	$(".vimeovideo").each(function(){
		var code = $(".code", this).text();
		//$(this).html('<object width="'+vid_w+'" height="'+vid_h+'"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id='+code+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id='+code+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="'+vid_w+'" height="'+vid_h+'"></embed></object>');
		$(this).html('<iframe src="http://player.vimeo.com/video/'+code+'?color=f0000c" width="'+vid_w+'" height="'+vid_h+'" frameborder="0"></iframe>');
	})
})

function loadHomePlayer(){
	var tot = $('#page-title li').length;
	$('#mainfeature li, #page-title li').hide();// , #mosaic li .preview .red-frame
	/*$('#mosaic li').mouseover(function(){
		clearInterval(player_timer);
		var ix = $('#mosaic li').index(this);
		playerMove(ix);
	});
	$('#mosaic li').mouseout(function(){
		clearInterval(player_timer);
		if(tot>1) player_timer = setInterval(function(){ playerMoveTime() }, 5000);
	});*/
	playerMove(0);
	if(tot>1) player_timer = setInterval(function(){ playerMoveTime() }, 5000);
}

function playerMoveTime(){
	var ix = $('#mainfeature li').index($('#mainfeature .cur')) + 1;
	playerMove(ix);
}

function playerMove(next){
	var tot = $('#page-title li').length;
	var current = $('#mainfeature li').index($('#mainfeature .cur'));
	var span = 124;
	var top = 0;
	
	if(next<0) next = tot-1;
	if(next>=tot) next = 0;
	
	if(next<current)
		c = 'up';
	if(next>current)
		c = 'down'; 
	if(next == current)
		c = false;
		
	switch(c){
		case 'up':
		var in_y = (top-span);
		var out_y = (top+span);
		break;
		
		case 'down':
		var in_y = (top+span);
		var out_y = (top-span);
		break;
	}
	
	if(c!=false){
		/* MAINFEATURE */
		$('#mainfeature li.cur').stop().animate({
															opacity: 0
														 });
		$('#mainfeature li:eq('+next+')').stop().show().css({
															opacity: 0
														 }).animate({
															opacity: 1
														 });
		$('#mainfeature li').removeClass('cur');
		$('#mainfeature li:eq('+next+')').addClass('cur');
		
		/* PAGE-TITLE */
		$('#page-title li.cur').stop().animate({
															top: out_y+'px',
															opacity: 0
														 });
		$('#page-title li:eq('+next+')').stop().show().css({
															top: in_y+'px',
															opacity: 0
														 }).animate({
															top: top+'px',
															opacity: 1
														 });
		$('#page-title li').removeClass('cur');
		$('#page-title li:eq('+next+')').addClass('cur');
		
		/* MOSAIC */
		//$('#mosaic li .preview .red-frame').stop().animate({opacity: 0});
		//$('#mosaic li:eq('+next+') .red-frame').stop().show().animate({opacity: 1});
		
	}
	return true;
}

var player_timer = 0;
$(window).load(function(){
	if($('#mainfeature li').length >1)
		loadHomePlayer();
	//else{
		$('#mosaic li').css({cursor: 'pointer'});
		$('#mosaic li .red-frame').css({opacity: 0});
		$('#mosaic li').mouseover(function(){
										   $(this).find('.red-frame').stop().animate({opacity: 1});
										   })
		$('#mosaic li').mouseout(function(){
										   $(this).find('.red-frame').stop().animate({opacity: 0});
										   })
		$('#page-title li:eq(0)').stop().show().css({
															top: 124+'px',
															opacity: 0
														 }).animate({
															top: 0+'px',
															opacity: 1
														 });
		$('#mosaic li').click(function(){
									   var url = $(this).find('.preview a').attr('href');
									   	window.location = url;
									   });
	//}
})


$(document).ready(function(){
	/* MENU */
	$('.trigger').click(function(){
		$(this).parent().find('.sub-navigation-list').toggle();
		return false;
	})
	/* GRID */
	$('#grid-mode').click(function(){
		$('#list-mode').removeClass('cur');
		$(this).addClass('cur');
		$('#mosaic').stop().animate({opacity: 0}, function(){
													$(this).removeClass('list');
													$(this).addClass('grid');
													setTimeout(function(){ $('#mosaic').stop().animate({opacity: 1}) }, 100);
													});
		return false;
	});
	$('#list-mode').click(function(){
		$('#grid-mode').removeClass('cur');
		$(this).addClass('cur');
		$('#mosaic').stop().animate({opacity: 0}, function(){
													$(this).removeClass('grid');
													$(this).addClass('list');
													setTimeout(function(){ $('#mosaic').stop().animate({opacity: 1}) }, 100);
													});
		return false;
	});
	
	$('#search_box').focus(function(){
								if($(this).val() == 'SEARCH FRAME.IT') $(this).val("");
								});
	$('#search_box').blur(function(){
								if($(this).val() == '') $(this).val("SEARCH FRAME.IT");
								});
	
	$('#adv_trigger').click(function(){
									 $('#advanced-search .adv').slideDown();
									 return false;
									 });
	
	setTimeout(function(){ $('.loremipsum div').hide().slideDown('slow'); }, 500);
})


function tagadv(){
	var newTagField = $("#search #search_box");
	var timeout = 0;
	newTagField.focus(function(){
					   var tmp_val = newTagField.val();
					   timeout = setInterval(function(){
												var val = newTagField.val();
												if((val.length > 0)&&(val!=tmp_val)){
													tmp_val = val;
													getTags(val);
					   							}
						}, 500);	
	});
	newTagField.blur(function(){
					   clearInterval(timeout);
					   timeout = setTimeout(function(){
														 $('#search .ajax_sugg').remove();
													  }, 500)
					   });
						
}

function getTags(val){
	val = val.split(" ");
	var ix = val.length - 1;
	val = val[ix];
	var request = [{
				   		name: "suggestion",
						method: "fe_search_suggestion",
						module: "cms/assets/suggestions",
						params:{
							'search': val
						}
				}];
			
	$.ajax({
		type: 'POST',
		cache: false,
		url: "/_ajax.php",
		processData: false,
		data: $.toJSON(request),
		dataType: 'json',
		success: function(json) {
			$('#search').find('.ajax_sugg').remove();
			$('#search').append(json.suggestion);
			$('#search').find(".row_sugg").click(function(){
												  var newTag = $(this).text();
												  var txt = $('#search #search_box').val();
												  $('#search #search_box').val(txt.replace(val, newTag));
												  $('#search .ajax_sugg').remove();
												  $('#search_form').submit();
												  })
		},
		error: function(er){
			// alert("no way!");
		}
	});	
	return false;
}
$(document).ready(function(){
	tagadv();
	$(".lightBox a").lightBox();
	$(".cnt_image_ref").lightBox();
	
	var cl_to = 0;
	$('#client_login_trigger a').click(function(){
		$('#client_login').stop().show().animate({height: 124+'px'});
		return false;
	});
	$('#client_login_bt').click(function(){
		var login = $('#client_login #login').val();
		var password = $('#client_login #password').val();
		var request = [{
				   		name: "login",
						method: "fe_client_login",
						params:{
							'login': login,
							'password': password
						}
				}];
			
		$.ajax({
			type: 'POST',
			cache: false,
			url: "/_ajax.php",
			processData: false,
			data: $.toJSON(request),
			dataType: 'json',
			success: function(json) {
				if(json.login.result)
					window.location = "/page/Wip/op/view/id/"+json.login.message;
				else
					alert(json.login.message);
			},
			error: function(er){
				// alert("no way!");
			}
		});	
		return false;
	});
	$('#client_login, #client_login_trigger').mouseout(function(){
		cl_to = setTimeout(function(){ $('#client_login').stop().animate({height: 0}) }, 2000);
	});
	$('#client_login, #client_login_trigger').mouseover(function(){
													   clearTimeout(cl_to)})
	
	$('.googlemap').each(function(){
		var address = $(this).text();
		$(this).append('<iframe width="670" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.it/maps?q='+address+'&amp;oe=utf-8&amp;client=firefox-a&amp;ie=UTF8&amp;hq=&amp;hnear='+address+'&amp;gl=it&amp;ei=RTq3TPDOB8LIswbggN2QCQ&amp;ved=0CBsQ8gEwAA&amp;t=h&amp;z=16&amp;output=embed"></iframe><br /><small><a href="http://maps.google.it/maps?q='+address+'&amp;oe=utf-8&amp;client=firefox-a&amp;ie=UTF8&amp;hq=&amp;hnear='+address+'&amp;gl=it&amp;ei=RTq3TPDOB8LIswbggN2QCQ&amp;ved=0CBsQ8gEwAA&amp;t=h&amp;z=16&amp;source=embed">View map</a></small>');
	});
})
