// JavaScript Document
/*--------------------------------------------------
Navigazione
--------------------------------------------------*/

function centerContent() {
	$(".doc_1 #container").css({ "left": -(sx_dx + mycenter) + "px" }, 500);
	$("#menu").css({ "left": (myWidth/2) + "px" }, 500);
}

function clickToGo() {
	var url = location.hash;
	var i = 0;
	if (url) {
			i = $("#mainMenu a").index($("a[href$=" + url + "]"));
		} else {
			i = 0;
		}
	$(".doc_1 #mainMenu a:eq(" + i + ")").parent('li').trigger('click');
}

function slideContent() {
	$(".doc_1 #menu1").click( function() {
		$("#container").stop().animate({ "top": -(0) + "px", "left": -(0+mycenter) + "px" }, 1250, "easeOutCubic");
		setTimeout(function() {
		$("#header").removeClass().addClass("color1");
		}
		,  600);
		sx_dx = 0;
	});
	$(".doc_1 #menu2").click( function() {
		$("#container").stop().animate({ "top": -(0) + "px", "left": -(2560+mycenter) + "px" }, 1250, "easeOutCubic");	
		setTimeout(function() {
		$("#header").removeClass().addClass("color2");
		}
		,  600);
		sx_dx = 2560;
	});
	$(".doc_1 #menu3").click( function() {
		$("#container").stop().animate({ "top": -(1440) + "px", "left": -(0+mycenter) + "px" }, 1250, "easeOutCubic");
		setTimeout(function() {
		$("#header").removeClass().addClass("color3")
		}
		,  600);
		sx_dx = 0;
	});
	$(".doc_1 #menu4").click( function() {
		$("#container").stop().animate({ "top": -(1440) + "px", "left": -(2560+mycenter) + "px" }, 1250, "easeOutCubic");
		setTimeout(function() {
		$("#header").removeClass().addClass("color4");
		}
		,  600);
		sx_dx = 2560;
	});
}

/*--------------------------------------------------
Slideshow
--------------------------------------------------*/
function slideshow(){
	increment = 0;
	slideSize = $("#slideshow .slideshow").size();
	var loop;

	function moveOn(parametro){
		$("#slideshow .slideshow_wrap").stop().animate({"top":-(328*parametro) + "px"}, 1200, "easeOutCirc");
		clearTimeout(loop);
		loop = setTimeout(function(){
			increment = increment + 1;
			if (increment >= slideSize){
				increment = 0;
			}
			moveOn(increment);
		},5000);
	}
	moveOn(increment);

	// frecce
	$("#slideshow .arrows .arr_next").click(function(){
		increment = increment + 1;
		if (increment >= slideSize){
			increment = 0;
		}
		moveOn(increment);
		return false;
	});

	$("#slideshow .arrows .arr_prev").click(function(){
		increment = increment - 1;
		if (increment < 0){
			increment = slideSize-1;
		}
		moveOn(increment);
		return false;
	});

}

/*--------------------------------------------------
slidingText
--------------------------------------------------*/

function slidingText() {
$('.scrollingtext').bind('marquee', function() {
		var ob = $(this);
		var tw = ob.width();
		var ww = ob.parent().width();
		var myLeft = tw;
			ob.css({ left: ww });
			ob.animate({ left: -(myLeft) + "px" }, 25000, 'linear', function() {
			ob.trigger('marquee');
	        });
	 }).trigger('marquee');
}

/*--------------------------------------------------
Percorso mappa
--------------------------------------------------*/
function percorso() {
$('#percorso ul li a').click(function() {
		$('#percorso ul li a').removeClass("on");
		$(this).addClass("on");
		$(".arrowPercorso").remove();
		$(this).parent().prepend("<span class='arrowPercorso'></span>");
	});
}


/*--------------------------------------------------
CONTATTI
--------------------------------------------------*/
function contatti(){
	$("#contForm #botContatti ul.topForm li:even").addClass("lfloat");
	$("#contForm #botContatti ul.topForm li:odd").addClass("rfloat");
	$("#contForm #botContatti ul.botForm li.bottoni input").hover( function() {
		$(this).addClass("on");	
	}, function() {
		$(this).removeClass("on");
	});
}

/*--------------------------------------------------
Checkbox
--------------------------------------------------*/
function check() {
$('ul.botForm li').each(function() {
		var t = $(this);
		t.find('input.checkbox').click(function() {
			t.find('span.checkbox').toggleClass( "on", this.checked );
		});
		$('#btn_reset').click(function(){	
 			$('input.checkbox').prev('span.checkbox').removeClass('on');
 			$('input.checkbox[checked=checked]').prev('span.checkbox').addClass('on');
		});	
	});
}

/*--------------------------------------------------
TWITTER
--------------------------------------------------*/
function twitter(){
	var RSS = "/wget.asp?url=http://twitter.com/statuses/user_timeline/unionmoda.rss"; //supah 14508180 utree 222172946 
	$.ajax({  
		type: "GET",
		url: RSS,
		dataType: "xml",
		contentType: "text/xml",
		success: parseXml,
		error: parseXml
	});
	
	function linkThis(tweet){
		var output = tweet.split(": ")[1];
		var twUser = /\@([a-z]+)/ig;
		var twUrl = /(https?:\/\/[\S]+)/;
		var twHash = /#([a-z]+)/ig;
		output = output.replace(twUrl, ' <a href="$1" target="_blank">$1</a> ');
		output = output.replace(twUser, ' <a href="http://twitter.com/$1" target="_blank">@$1</a> ');
		output = output.replace(twHash, ' <a href="http://twitter.com/search?q=%23$1" target="_blank">#$1</a> ');
		return output;
	}
	function parseTwitterDate($stamp)
	{		
	// convert to local string and remove seconds and year //		
		var date = new Date(Date.parse($stamp)).toLocaleString();
		var i = date.indexOf("201");
		if (i > -1) {date = date.substr(0,i)};
		return date;
	}
	
	function parseXml(xml) {
		//$(".loading").remove();
		$(xml).find("item").each(function(i) {
			if (i < 10) {
				$("#ticker2 .maskTw").append('<div class="item"><div class="data">' +parseTwitterDate($(this).find("pubDate").text()) + '</div><div class="tweet">' + linkThis($(this).find("title").text()) + '</div></div>');
				loop = setInterval(function(){
					$("#ticker2 .maskTw .item:eq(0), #ticker2 .maskTw .item:eq(1), #ticker2 .maskTw .item:eq(2)").fadeOut(800,function(){
						$(this).appendTo($("#ticker2 .maskTw")).show();
					});
				},10000);
			}
		});
	}
}

/*--------------------------------------------------
BLOG
--------------------------------------------------*/
function blog(){
	var RSS = "/wget.asp?url=http://www.unionmoda.com/blog/feed/"; //supah 14508180 utree 222172946 
	$.ajax({  
		type: "GET",
		url: RSS,
		dataType: "xml",
		contentType: "text/xml",
		success: parseXml,
		error: parseXml
	});
	
	function linkThis(tweet){
		var output = tweet.split(": ")[1];
		var twUser = /\@([a-z]+)/ig;
		var twUrl = /(https?:\/\/[\S]+)/;
		var twHash = /#([a-z]+)/ig;
		output = output.replace(twUrl, ' <a href="$1" target="_blank">$1</a> ');
		return output;
	}
	function parseTwitterDate($stamp)
	{		
	// convert to local string and remove seconds and year //		
		var date = new Date(Date.parse($stamp)).toLocaleString();
		var i = date.indexOf("201");
		if (i > -1) {date = date.substr(0,i)};
		return date;
	}
	function parseDescr(descr) {
			if (descr.length > 200)
			{
				return descr.substr(0, 199);
			}
			else {return descr;}
		}
	
	function parseXml(xml) {
		//$(".loading").remove();
		$(xml).find("item").each(function(i) {
			if (i < 10) {
				$("#ticker3 .maskTw").append('<div class="item"><div class="data">' +parseTwitterDate($(this).find("pubDate").text()) + '</div><div class="blogtitle">' +$(this).find("title").text() + '</div><div class="tweet">' + parseDescr($(this).find("description").text()) + ' [...]<a target="_blank" href="'+ $(this).find("link").text() +'"><br/><br/>Leggi tutto</a></div></div>');
				loop = setInterval(function(){
					$("#ticker3 .maskTw .item:eq(0)").fadeOut(800,function(){
						$(this).appendTo($("#ticker3 .maskTw")).show();
					});
				},10000);
			}
		});
	}
}

/*--------------------------------------------------
BRAND
--------------------------------------------------*/
function brand() {
	var classes = new Array('w600','w580', 'w560', 'w540','w520','w500','w520','w540', 'w560','w580');
	var n = classes.length;
	numeroclassi = n;
	var numels = $(".int_dx ul li.brand").size();
	ii = 0
	for (i = 0; i < numels;i++)
	{
		$(".int_dx ul li.brand:eq("+i+")").addClass(classes[ii]);
		ii++;
		if (ii==n)
		{ii = 0;}
	}
	$(".int_dx ul li.brand a:odd").addClass("odd");
	$(".int_dx ul li.brand a:even").addClass("even");
	$(".int.brand .int_dx ul li.brand a").append("<span>&rsaquo;</span>");
	$(".int.sb .int_dx ul li.brand a").append("<span>&lsaquo;</span>");
	/*$(".int.brand .int_dx ul li.brand").css({"padding-right": "35px"});
	$(".int.brand .int_dx ul li.brand").hover( function(){
		$(this).stop().animate({"padding-right": "0px"}, 200, "easeOutCirc");
		$(this).find("span").animate({"right": "30px"}, 200, "easeOutCirc");
	}, function() {
		$(this).stop().animate({"padding-right": "35px"}, 200, "easeOutCirc");
		$(this).find("span").animate({"right": "20px"}, 200, "easeOutCirc");
	});*/
}

/*--------------------------------------------------
SOCIAL BUTTON
--------------------------------------------------*/
function socialButton() {
$("#socialMenu #more").click(function() {
		if($("#socialMenu #cloud").is(":visible")) {
			$("#socialMenu #cloud").animate({"left": "20px", "opacity": "0"}, 600, "easeOutCirc", function(){$("#socialMenu #cloud").hide()});
		} else {
			$("#socialMenu #cloud").show().stop().animate({"left":"88px", "opacity": "1"}, 1250, "easeOutCirc");
		} 
		return false;
	});
}

/*--------------------------------------------------
NEWSLETTER
--------------------------------------------------*/
function newsletterForm(){
	$(".formNewsletter ul.topForm li:even").addClass("lfloat");
	$(".formNewsletter ul.topForm li:odd").addClass("rfloat");
	$(".formNewsletter ul.botForm li.bottoni input").hover( function() {
		$(this).addClass("on");	
	}, function() {
		$(this).removeClass("on");
	});
}

/*--------------------------------------------------
GRAZIE
--------------------------------------------------*/

/*function fancy_grazie(){
	if( ($("#ut_nome").val() != ""))  {
	$("#actionVuota").append('<div id="actionFancy">I dati sono stati inviati correttamente, grazie per averci contattato.</div>');
	$.fancybox( {content: $("#actionFancy"), 'overlayColor'  : '#fff', 'transitionIn'	: 'fade', 'transitionOut'	: 'fade', 'titleShow' : false });
	}
}*/

function fancy_grazie(){
	if( ($("#ut_nome").val() != ""))  {
	$("#actionVuota").append('<div id="actionFancy">I dati sono stati inviati correttamente, grazie per averci contattato.</div>');
	$.fancybox( {content: $("#actionFancy"), 'overlayColor'  : '#fff', 'transitionIn'	: 'fade', 'transitionOut'	: 'fade', 'titleShow' : false, onClosed : deleteInput });
	}
}

function deleteInput() {
	$("form input:text, form textarea").attr("value","");
}

/*--------------------------------------------------
PULSONE
--------------------------------------------------*/
function add_google_plusone(){
	try {
		if (gapi.plusone){
			var content_lang = $('head meta[http-equiv=content-language]').attr('content');
			window.___gcfg = {
        lang: content_lang,
				parsetags: 'explicit'
      };
			gapi.plusone.render("plusone_div", {"size": "medium", "count": "true", "lang": content_lang, "href": location.href});	
		}else{
		}
	}
	catch(e){
	}
}
/*--------------------------------------------------
INIT
--------------------------------------------------*/
try{
	var myHeight = $(window).height();
	var myWidth = $(window).width();
	var mycenter = parseInt(1280 - (myWidth / 2),10);
	var sx_dx = 0;
}
catch(e){}

$(document).ready(function ()
{
		myHeight = $(window).height();
		myWidth = $(window).width();
		mycenter = (1280 - (myWidth / 2));
		$(".doc_1 #wrap").css({"height": myHeight + "px"});
		slideContent();
		centerContent();
		clickToGo();
		twitter();
		blog();
		brand();
		check();
		percorso();
		contatti();
		socialButton();
		add_google_plusone();
		newsletterForm();
		setTimeout(slidingText , 600); // setTimeout per permettere il rendering del font
		// slideshow();
		$(".scrollable").scrollable({ vertical: true, circular: true, speed: 1200, prev: "#slideshow .arr_prev", easing: "easeOutCirc", next: "#slideshow .arr_next"}).autoscroll({ autoplay: true, interval:	5000 });
		$(".fancybox").fancybox({
			  'overlayColor'  : '#fff',
			  'transitionIn'	: 'fade',
			  'transitionOut'	: 'fade',
			  'titleShow' : false
		});
});

$(window).resize(function () {
		myHeight = $(window).height();
		myWidth = $(window).width();
		mycenter = (1280 - (myWidth / 2));
		centerContent();
	$(".doc_1 #wrap").css({"height": myHeight + "px"});
});
