$(window).load(function(){
	setLayout();
	loadXML();
});


function setLayout(){
	h_wp=$(window).height()-29;
	w_wp=$(window).width();
	$('#layout').height(h_wp);
	$('#footer').css({top:h_wp, visibility:'visible'});
	$('#container').css({ top:(h_wp - 592)/2, left: (w_wp - 1000)/2 });
	$('#container_gallery').css({ top:(h_wp - 673)/2, left: (w_wp - 1032)/2 });
	hp.open();
			
	$(window).resize(function(){
		h_wp=$(window).height()-29;
		w_wp=$(window).width();
		$('#layout').height(h_wp);
		$('#footer').css({top:h_wp});
		$('#container').css({top:(h_wp - 592)/2, left: (w_wp - 1000)/2 });
		$('#container_gallery').css({ top:(h_wp - 673)/2, left: (w_wp - 1032)/2 });
	});	
};

function listenEvents() {
	$('#container a.video').click(function(e) {
			e.preventDefault();
			video.open();
	});
	$('#container a.gallery').click(function(e) {
			e.preventDefault();
			hp.close();
	});
}

var hp = {
	open: function() {
		$('#container').fadeIn(1200, function() {
			$('#container .dolceVita').fadeIn('slow', function() {
				$('#txt').fadeIn('slow', function() {
					$('#container div#video-link').fadeIn('slow', function() {
						$('#container div#gallery-link').fadeIn('slow', function() {
							listenEvents();
						});
					});
				});
			});
		});
	},
	close: function() {
		$('#container div#gallery-link').fadeOut('slow');
		$('#container div#video-link').fadeOut('slow', function() {
			//$('div#video-link').fadeOut('slow', function() {
				$('#txt').fadeOut('slow', function() {
					$('#container .dolceVita').fadeOut('slow', function() {
						$('#container').fadeOut(1200, function() {
							$('#container a.video, #container a.gallery').unbind();
							gallery.open();
						});
					});
				});
			//});
		});
	}
}


var video = {
	width: 520,
    height: 288,
	open : function() {
 		backgroundDiv = "<div class='overlay'></div>";
		videoDiv = "<div class='videoHolder'><div id='videoWrapper'></div><div id='closeWrapper'><a href='#' id='closeLayer' /></a></div></div>";
		$('#layout').append(backgroundDiv);
		$('#layout').append(videoDiv);
		$('div.overlay').css('height',$('#layout').height());
		$('div.overlay').css('opacity',0);
		$('div.videoHolder').css({top:(h_wp - 224)/2, left: (w_wp - 540)/2 });
		$('div.overlay').fadeTo('slow',0.8, function() {
			$('div.videoHolder').fadeIn('slow', function() {
				$('#videoWrapper').media({
                    width: video.width,
                    height: video.height,
					bgColor: 'transparent',
					autoplay:  true,
					params: { wmode : 'transparent',scale:'noscale', allowfullscreen:'true'},
					attrs :	{ id : 'player', wmode : 'transparent',scale:'noscale', allowfullscreen:'true' },
					src:	'swf/player.swf',
					flashvars:  {file: 'voddolcegabbana/cannes2009.flv', streamer: 'rtmp://cp70242.edgefcs.net/ondemand', skin: 'swf/stylish.swf', autostart: true, controlbar: 'over', frontcolor :"ffffff", backcolor: "000000", lightcolor : "FFFFFF", overstretch : false,bufferlength : 5, usefullscreen : true},
					caption: false
				});
				$('a#closeLayer').bind('click', function(){ video.close();});
			});
		});
	},
	close : function() {
		$('#videoWrapper').fadeOut('slow', function() {
			$('div.videoHolder').fadeOut('slow', function() {
				$('div.overlay').fadeOut('slow', function() {
					$('#videoWrapper').remove();
					$('div.videoHolder').remove();
					$('div.overlay').remove();
				});
			});
		});
	}
}

var gallery = {
	open : function() {
		$('#container_gallery').fadeIn(1200, function() {
			$('#container_gallery .dolceVita').fadeIn('slow', function() {
				$('#container_gallery div.video-link').fadeIn('slow', function() {
					$('#container_gallery div.gallery-link').fadeIn('slow', function() {
						gallery.init();
					});
				});
			});
		});
	},
	close : function() {
		$('#imageWrapper, #imageInfo').fadeOut('slow');
		$('#container_gallery div.video-link').fadeOut('slow');
		$('#container_gallery div.gallery-link').fadeOut('slow', function() {
			$('#container_gallery .dolceVita').fadeOut('slow', function() {
				$('#container_gallery').fadeOut(1200, function() {
					$('#container_gallery a.video, #container_gallery .dolceVita a').unbind();
					hp.open();
				});
			});
		});
	},
	init : function() {
		$('p.caption').text('');
		gallery.currItem = 1;
		$('.imgCounter span#total').text(gallery.items.length);
		$('#imageWrapper, #imageInfo').fadeIn('slow');
		gallery.openImg(gallery.currItem);
		$('#container_gallery a.video').click(function(e) {
			e.preventDefault();
			video.open();
		});
		$('#container_gallery .dolceVita a').click(function(e) {
			e.preventDefault();
			gallery.close();
		});
	},
	openImg : function(n) {
		gallery.currItem = n = (n == 0) ? gallery.items.length : (n > gallery.items.length) ? 1 : n;
		var pic = new Image();
		picSrc = $(gallery.items).eq(gallery.currItem-1).attr('src');
		caption = $(gallery.items).eq(gallery.currItem-1).attr('caption');
		$(pic).load(function(){
			$('p.caption').text(caption);
			$('#imgHolder')
				.append(pic)
				.find('img')
				.fadeIn('slow', function() {
					if($('#imgHolder img').length > 1) {  //remove first image from DOM
						$('#imgHolder img:first').remove();
						}
					$('.imgCounter span#current').text(gallery.currItem);
					});
				});
		$(pic).attr({src:imgPath + picSrc});
		},
	browse : function(n) {
		gallery.openImg(gallery.currItem + n);
		}
}

function MM_showHideLayers() { 
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'block':(v=='hide')?'none':v; }
    obj.display=v; }
}

function loadXML() {
	$.ajax({
        type: "GET",
        url: "gallery.xml",
        dataType: "xml",
        success: function(xmlData) {
			gallery.xmlFile = xmlData;
			},
		complete : function(xmlData) {
			gallery.items = $(gallery.xmlFile).find('image');
			},
		error:function(){
			alert('Error loading xml data. Please, reload this page and try again.')
			}	
		});
	}
	
var imgPath = 'img/gallery/images/';

/********************** UTILS ************************/
//media
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(6($){$.3.4=6(d,e,f){s X.1o(6(){5(10 d==\'6\'){f=e;e=d;d={}}2 o=22(X,d);5(10 e==\'6\')e(X,o);2 r=28();2 m=r.3C(o.8)||[\'\'];o.L?m[0]=o.L:m.37();v(2 i=0;i<m.11;i++){3=m[i].1F();5(1a(3[0]))3=\'3\'+3;5(!$.3.4[3])2x;2 a=$.3.4[3+\'1v\'];5(!o.u)o.u={};5(a){2 b=a.12==\'1b\';o.u[a.12||\'18\']=b?(o.18?1:0):o.18?19:1Y}2 c=$.3.4[3](X,o);c.1V(\'1S\',o.G).y(o.y);5(10 f==\'6\')f(X,c[0],o,a.H);36}})};$.3.4.2Z=6(a,b){5(!a||!b||!$.3.4.t.F[b])s;a=a.1F();5(1a(a[0]))a=\'3\'+a;$.3.4[a]=$.3.4[b];$.3.4[a+\'1v\']=$.3.4.t.F[b]};$.3.4.t={y:1J,z:1J,1H:1,18:0,G:\'#2K\',u:{},P:{},D:{},1i:\'7\',1p:Z,25:\'1B.Y\',26:\'1B.Y\',A:{1w:\'19\',1z:\'19\',1x:\'24\',13:\'0.9\',1A:Z,1s:Z,1c:Z,1q:Z}};$.3.4.t.F={S:{H:\'S\',J:\'20,1e,Y\',N:{17:\'16:3q-3n-1T-3i-3g\',L:\'T/x-1P\',1O:\'Q://35.32.R/2X/1M/2V/S/2U.1K#13=\'+$.3.4.t.1i},M:{L:\'T/x-1M-S\',15:\'Q://U.2O.R/2M/2L\'}},1k:{H:\'1k\',J:\'2J,2I,2G,2F,2D,2C,2B,2A,2y,2w,2u,2t,2s,2r,2p,2n,2l,2k,2h,2g,2e,4l,4e,4b\',N:{17:\'16:4a-49-46-45-41\',1O:\'Q://U.27.R/3Y/3U.1K\'},M:{15:\'Q://U.27.R/1k/3S/\'}},3R:{H:\'23\',J:\'3Q,3P,3O,3N,3L,3J,3G\',12:\'1b\',N:{17:\'16:3F-3E-1T-3D-3B\'},M:{L:\'3A/x-3z-3y-3x\',15:\'Q://U.23.R/3v/\'}},1X:{H:\'1X\',J:\'3u,3t,3s,3r\',12:\'1b\',1W:\'3p\',N:{17:\'16:3o-3m-3l-3k-3j\',L:\'T/x-1P\'},M:{L:$.1j.3h&&1Q()?\'T/x-3f-3e\':\'T/x-3d\',15:\'Q://U.3c.R/3b/3a/\'}},W:{H:\'W\',J:\'V,39\'},A:{H:\'A\',J:\'14\'}};6 1Q(){2 a=38.34;v(i=0;i<a.11;i++){2 b=a[i];5(b[\'33\']==\'31-30.2Y\')s 19}s 1Y}2 l=1;v(2 n C $.3.4.t.F){2 q=$.3.4.t.F[n].J;$.1o(q.2W(\',\'),6(i,o){5(1a(o[0]))o=\'3\'+o;$.3.4[o]=$.3.4[n]=1L(n);$.3.4[o+\'1v\']=$.3.4.t.F[n]})};6 28(){2 a=\'\';v(2 b C $.3.4.t.F){5(a.11)a+=\',\';a+=$.3.4.t.F[b].J};s 1N 2T(\'\\\\.(\'+a.2S(/,/g,\'|\')+\')\\\\b\')};6 1L(c){s 6(a,b){s 1l(a,b,c)}};6 1a(c){s\'2R\'.2Q(c)>-1};6 22(d,e){e=e||{};2 f=$(d);2 g=d.2P||\'\';2 j=$.1m?f.1m():$.1I?f.2N():{};j=j||{};2 w=j.y||1G(((g.1R(/w:(\\d+)/)||[])[1]||0));2 h=j.z||1G(((g.1R(/h:(\\d+)/)||[])[1]||0));5(w)j.y=w;5(h)j.z=h;5(g)j.K=g;2 a=$.3.4.t;2 b=(($.1I||$.1m)&&$.3.4.t.1H)?e:j;2 c=b==e?j:e;2 p={u:{G:e.G||$.3.4.t.G}};2 k=$.1n({},a,b,c);$.1o([\'P\',\'u\',\'D\',\'A\'],6(i,o){k[o]=$.1n({},p[o]||{},a[o]||{},b[o]||{},c[o]||{})});5(10 k.I==\'1U\')k.I=f.2H();k.8=k.8||f.1u(\'3w\')||f.1u(\'8\')||\'2E\';s k};$.3.4.Y=6(b,c){5(10 1Z==\'1U\'){5(c.D){2 a=[];v(2 f C c.D)a.E(f+\'=\'+c.D[f]);5(!c.u)c.u={};c.u.D=a.1f(\'&\')}s 1l(b,c,\'S\')}2 d=b.B?(\' B="\'+b.B+\'"\'):\'\';2 e=c.K?(\' 1h="\'+c.K+\'"\'):\'\';2 g=$(\'<O\'+d+e+\'>\');$(b).1g(g).1t();2 h=1N 1Z(c.8,\'2z\'+l++,c.y,c.z,c.1i,c.G);5(c.1p)h.3H(c.1p);v(2 p C c.u)5(p!=\'G\')h.3I(p,c.u[p]);v(2 f C c.D)h.2v(f,c.D[f]);h.3K(g[0]);5(c.I)$(\'<O>\').1d(g).V(c.I);s g};$.3.4.20=$.3.4.1e=6(a,b){2 c=b.8;2 d=/\\.1e\\b/i.3M(c)?$.3.4.t.26:$.3.4.t.25;b.8=d;b.8=b.8+\'?1E=\'+c;b.D=$.1n({},{1E:c},b.D);s $.3.4.Y(a,b)};$.3.4.14=6(a,b){5(!21.1r||!21.1r.1y){5($.3.4.14.1C)s;$.3.4.14.1C=1;2q(\'2o 3T 2m 3V 1y.3W 3X.\');s}2 c={y:b.y,z:b.z,2j:b.G,1w:b.A.1w,1z:b.A.1z,1x:b.A.1x,13:b.A.13};2 d={1A:b.A.1A,1s:b.A.1s};2 e=a.B?(\' B="\'+a.B+\'"\'):\'\';2 f=b.B||\'2i\'+l++;2 g=b.K?(\' 1h="\'+b.K+\'"\'):\'\';2 h=$(\'<O\'+e+g+\'>\');$(a).1g(h).1t();1r.1y.3Z({40:b.8,1c:b.A.1c,1q:b.A.1q,B:f,2f:h[0],42:c,43:d});5(b.I)$(\'<O>\').1d(h).V(b.I);s h};6 1l(b,c,d){2 e=$(b);2 o=$.3.4.t.F[d];5(d==\'W\'){2 o=$(\'<W\'+\' y="\'+c.y+\'" z="\'+c.z+\'" >\');o.1u(\'8\',c.8);o.1V(\'1S\',o.G)}1D 5($.1j.2d){2 a=[\'<4h y="\'+c.y+\'" z="\'+c.z+\'" \'];v(2 f C c.P)a.E(f+\'="\'+c.P[f]+\'" \');v(2 f C o.N||{})a.E(f+\'="\'+o.N[f]+\'" \');a.E(\'></4d\'+\'4c\'+\'>\');2 p=[\'<2c H="\'+(o.1W||\'8\')+\'" 2b="\'+c.8+\'">\'];v(2 f C c.u)p.E(\'<2c H="\'+f+\'" 2b="\'+c.u[f]+\'">\');2 o=2a.29(a.1f(\'\'));v(2 i=0;i<p.11;i++)o.48(2a.29(p[i]))}1D{2 a=[\'<47 y="\'+c.y+\'" z="\'+c.z+\'" 4f="4g:44"\'];5(c.8)a.E(\' 8="\'+c.8+\'" \');v(2 f C c.P)a.E(f+\'="\'+c.P[f]+\'" \');v(2 f C o.M||{})a.E(f+\'="\'+o.M[f]+\'" \');v(2 f C c.u)a.E(f+\'="\'+c.u[f]+\'" \');a.E(\'></4i\'+\'4j\'+\'>\')}2 g=b.B?(\' B="\'+b.B+\'"\'):\'\';2 h=c.K?(\' 1h="\'+c.K+\'"\'):\'\';2 j=$(\'<O\'+g+h+\'>\');e.1g(j).1t();($.1j.2d||d==\'W\')?j.4k(o):j.V(a.1f(\'\'));5(c.I)$(\'<O>\').1d(j).V(c.I);s j}})(4m);',62,271,'||var|fn|media|if|function||src||||||||||||||||||||return|defaults|params|for|||width|height|silverlight|id|in|flashvars|push|players|bgColor|name|caption|types|cls|type|eAttrs|oAttrs|div|attrs|http|com|flash|application|www|html|iframe|this|swf|null|typeof|length|autoplayAttr|version|xaml|pluginspage|clsid|classid|autoplay|true|isDigit|autostart|initParams|appendTo|mp3|join|after|class|flashVersion|browser|quicktime|generate|metadata|extend|each|expressInstaller|userContext|Sys|onLoad|remove|attr|_player|inplaceInstallPrompt|framerate|Silverlight|isWindowless|onError|mediaplayer|warning|else|file|toLowerCase|parseInt|preferMeta|meta|400|cab|getGenerator|shockwave|new|codebase|oleobject|isFirefoxWMPPluginInstalled|match|backgroundColor|11cf|undefined|css|oUrl|winmedia|false|SWFObject|flv|window|getSettings|real||flvPlayer|mp3Player|apple|getTypesRegExp|createElement|document|value|param|msie|tiff|parentElement|tif|snd|AG|background|qti|qif|include|qtif|You|qt|alert|psd|m4a|mp4|mpeg|addVariable|mpg|continue|midi|movie_player_|mid|mov|gsm|bmp|unknown|au|aac|text|aiff|aif|ffffff|getflashplayer|go|data|adobe|className|indexOf|0123456789|replace|RegExp|swflash|cabs|split|pub|dll|mapFormat|mswmp|np|macromedia|filename|plugins|fpdownload|break|shift|navigator|pdf|MediaPlayer|Windows|microsoft|mplayer2|wmp|ms|444553540000|mozilla|96b8|00C04F79FAA6|B153|11d3|394A|ae6d|6BF52A52|url|d27cdb6e|wmv|wma|avi|asf|player|href|plugin|realaudio|pn|audio|0020AFBBCCFA|exec|B84B|8BE4|CFCDAA03|smil|useExpressInstall|addParam|smi|write|rv|test|rpm|rm|ram|ra|realplayer|download|must|qtplugin|the|js|script|qtactivex|createObjectEx|source|D3488ABDDC6B|properties|events|block|BC80|4B23|embed|appendChild|8C17|02BF25D5|3gp|ject|ob|3g2|style|display|object|em|bed|append|wav|jQuery'.split('|'),0,{}))