$(document).ready(function(){

(function ($) {
$.fn.cross = function (options) {
	return this.each(function (i) { 
	var $$ = $(this);
	
	var target = $$.css('backgroundImage').replace(/^url|[\(\)'"]/g, '');
	
	$$.wrap('<span style="position: relative;"></span>')
	
	.parent()
	
	.prepend('<img>')
	
	.find(':first-child')
	
	.attr('src', target);
	
	if ($.browser.mozilla) {
		$$.css({
			'position' : 'absolute', 
			'left' : 0,
			'background' : '',
			'top' : this.offsetTop
		});
	} else if ($.browser.opera && $.browser.version < 9.5) {                 
		$$.css({
			'position' : 'absolute', 
			'left' : 0,
			'background' : '',
			'top' : "0"
		});
	} else { // Safari
		$$.css({
			'position' : 'relative', 
			'top' : -98,
			'background' : ''
		});
	}
	
	$$.hover(function () {
		$$.stop().animate({opacity: 0}, 250);}, function () {$$.stop().animate({opacity: 1}, 250);});});
	};

})(jQuery);



$(window).bind('load', function () {
	$('img.fade').cross();
	$('#indietro').fadeOut(0)
});

var pagine = new Array("index.html","02.html","03.html","04.html","05.html","06.html", "07.html" ,"08.html","09.html","10.html","11.html","12.html","13.html","14.html","15.html","16.html");
var corrente = 0;


$(".avanti").attr({ href: pagine[Number(corrente)+1]});
$(".indietro").attr({ href: pagine[Number(corrente)-1]});
$(".share_fb").attr({ href: "http://www.facebook.com/sharer.php?u=http%3A%2F%2Fcollection.misssixty.com/sunglasses/"+pagine[Number(corrente)]});

$("#grande").css('width',"723px")
$("#grande").css('top',"0px")
$("#grande").css('left',"0px")



$('.thumb a').click(function(){
	var page = $(this).attr('href');
	corrente = Number($(this).attr('title'));
	$(".share_fb").attr({ href: "http://www.facebook.com/sharer.php?u=http%3A%2F%2Fcollection.misssixty.com/sunglasses/"+pagine[Number(corrente)]});	if(corrente>=15){
		$('#avanti').fadeOut(1000)
		$('#indietro').fadeIn(1000)
	}else if(corrente<=15){
		$('#avanti').fadeIn(1000)
		$('#indietro').fadeIn(1000)
	}
	
	if(corrente==0){
		$('#avanti').fadeIn(1000)
		$('#indietro').fadeOut(1000)
	}

	
	$(".avanti").attr({ href: pagine[Number(corrente)+1]});
	$(".indietro").attr({ href: pagine[Number(corrente)-1]});
	
	$(".thumb").attr({id:""});
	$(this).parent().attr({id:"selezionato"});
	
	$('#destra').fadeOut(1000,function(){$('#destra').load(pagine[Number(corrente)]+' #immagine',function(){$("#grande").css('width',"723px"), $("#grande").css('top',"0px"), $("#grande").css('left',"0px"),$(this).fadeIn(1000,function(){$("img.fade").cross()})})});
	$('#descrizione').fadeOut(1000,function(){$('#descrizione').load(pagine[Number(corrente)]+' #contenuto, #dettagli', function(){$(this).fadeIn(1000)})});
	return false;
});

$('.avanti').click(function(){
	var page_avanti = $(this).attr('href');
	if(corrente>=14){
		$('#avanti').fadeOut(1000)	
	}else{
		$('#avanti').fadeIn(1000)
	}

	$('#destra').fadeOut(1000,function(){$('#destra').load(page_avanti + ' #immagine',function(){$("#grande").css('width',"723px"), $("#grande").css('top',"0px"), $("#grande").css('left',"0px"), $(this).fadeIn(1000,cabbak)})});
	$('#descrizione').fadeOut(1000,function(){$('#descrizione').load(page_avanti +' #contenuto, #dettagli', function(){$(this).fadeIn(1000,function(){$("img.fade").cross()})})});
	$('#indietro').fadeIn(1000)
	return false;
});

$('.indietro').click(function(){
	var page_indietro = $(this).attr('href');
	$('#avanti').fadeIn(1000)
	if(corrente==1){
		$('#avanti').fadeOut(1000)
	}
	$('#destra').fadeOut(1000,function(){$('#destra').load(page_indietro + ' #immagine',function(){$("#grande").css('width',"723px"), $("#grande").css('top',"0px"), $("#grande").css('left',"0px"), $(this).fadeIn(1000,cattak)})});
	$('#descrizione').fadeOut(1000,function(){$('#descrizione').load(page_indietro +' #contenuto, #dettagli', function(){$(this).fadeIn(1000,function(){$("img.fade").cross()})})});
	
	return false;
});

function cabbak() {
	corrente++
	$(".thumb").attr({id:""});
	$(".share_fb").attr({ href: "http://www.facebook.com/sharer.php?u=http%3A%2F%2Fcollection.misssixty.com/sunglasses/"+pagine[Number(corrente)]});
	$("."+Number(corrente+1)).attr({id:"selezionato"});
	$(".avanti").attr({ href: pagine[Number(corrente+1)]});
	$(".indietro").attr({ href: pagine[Number(corrente-1)]});
}

function cattak() {
	corrente--
	$(".thumb").attr({id:""});
	$(".share_fb").attr({ href: "http://www.facebook.com/sharer.php?u=http%3A%2F%2Fcollection.misssixty.com/sunglasses/"+pagine[Number(corrente)]});
	$("."+Number(corrente+1)).attr({id:"selezionato"});
	$(".avanti").attr({ href: pagine[Number(corrente+1)]});
	$(".indietro").attr({ href: pagine[Number(corrente-1)]});
}

$('#sopra').click(
	function() {
	if($("#grande").height()==1044){
		$("#immagine_txt").attr({src:"../immagini/piu.png"});
		$("#grande").fadeTo(500,0,callback);	
	}else{
		$("#immagine_txt").attr({src:"../immagini/meno.png"});
		$("#grande").fadeTo(500,0,callback2);
	}
})

function callback(){
	$("#grande").css('width',"723px")
	$("#grande").css('top',"0px")
	$("#grande").css('left',"0px")
	$("#grande").fadeTo(500,1);
}

function callback2(){
	$("#immagine_grande").attr({ onmousemove: "posizione(event)"});
	$("#grande").css('width',"1452px")
	$("#grande").css('top',"0px")
	$("#grande").css('left',"-380px")
	$("#grande").fadeTo(500,1);
}

});
