var	start_day         = 7,
	start_date        = 200910,
	end_date          = 201010,
	start_month       = 10,
	total_months      = (start_month+11),
	total_days        = '',
	current_day       = new Date().getDate(),
	current_month     = new Date().getMonth()+1,
	current_year      = new Date().getYear(),
	current_full_year = new Date().getFullYear(),
	str_months        = new Array('jan','fev','mar','abr','mai','jun','jul','ago','set','out','nov','dez'),
	obj_content       = new Array(),
	obj_content_size,
	month_list        = $('#months'),
	days_list         = $('#days'),
	campaign_content  = $('#campaign_content'),
	flash_content     = $('#flash_content'),
	title_content     = $('#campaign_content h3'),
	adjust_position   = ($('html').width()-$('#container').width())/2,
	tooltip           = '<span>aguarde...</span>',
	css               = ' class="empty"',
	status            = 0,
	html              = '',
	pre_html          = '',
	pos_html          = '',
	delay             = 200,
	params            = {allowScriptAccess: "always", wmode: "transparent"},
	numCurrentTime    = '',
	numGetDuration    = '',
	after_day         = 0,
	actual_date       = '',
	cont              = '';

var PNG_FIX = function(){

	$("h1 a, h2, .bt_conheca_o_hot_site_do_linea, .bt_fiat").addClass("pngfix");
	if(!/msie [^6]\.0/i.test(navigator.userAgent) && /msie 6\.0/i.test(navigator.userAgent)){ DD_belatedPNG.fix("img, .pngfix"); };

};

var EXTERNAL_LINK = function(){

	$("a[rel='external-link']").attr('target','_blank');

};

var BUILD_CONTENTS = {

	_stored_elements: function(){

		$.ajax({
			url: "/365/xml/motivos.xml",
			type: "GET",
			dataType: "html",
			contentType: "application/x-www-form-urlencoded; charset=UTF-8",
			error: function(msg){
				alert('Erro em stored_content: '+msg);
			},
			beforeSend: function(msg){
				//
			},
			success: function(xml){

				$.xmlDOM(xml).find('motivo').each(function(n){

					var date = $(this).find('data').text().split('/'),
						d    = parseFloat(date[0]),
						m    = parseFloat(date[1]),
						fy   = parseFloat(date[2]),
						t    = $(this).find('titulo').text(),
						p    = $(this).find('url').text();

					obj_content.push([d, m, fy, t, p]);

				});
			},
			complete: function(msg){
				obj_content_size  = obj_content.length;
				BUILD_CONTENTS._build_months();
			}
		});

	},

	_build_months: function(){

		var cont=0;

		for(var i=start_month; i<=total_months; i++){		
			var m  = i < 13 ? i : i-12,
				y  = i < 13 ? 2009 : 2010,
				a  = current_month == m && current_full_year == y ? 'active' : '',
				c,
				t;

			if(current_full_year==2009){
				c = i > current_month ? css : '';
				t = i > current_month ? tooltip : '';
			}else{
				c = (i-12) > current_month ? css : '';
				t = (i-12) > current_month ? tooltip : '';
			};

			html+="<li"+ c +">"+
				  "<a href='#"+m+"/"+y+"' title='"+m+"/"+y+"' class='"+a+"'>"+str_months[m-1]+" "+ t +"</a>"+
				  "</li>\n";
		};
		month_list.html(html);

		html = '';

		month_list.find('li a').unbind('click').click(function(){
			if($(this).parent().hasClass('empty')) return false;

			var h = $(this).attr('href').split('#')[1].split('/'), m = h[0], y = h[1];

			month_list.find('li a').removeClass('active');

			$(this).addClass('active');
			
			BUILD_CONTENTS._build_days(parseInt(m),parseInt(y));

			return false;
		});

		month_list.find('li a.active').click();

	},

	_build_days: function(m,y){
		days_list.html('');
	
		if(cont==0) days_list.css('opacity', 0);
	
		var yy = (y==2009)?109:110, o_t, o_p;

		switch(m){
			case 1: case 3: case 5: case 7: case 8: case 10: case 12 : total_days = 31 ; break
			case 4: case 6: case 9: case 11: total_days = 30 ; break
			case 2:
				if(yy%400==0 || yy%4==0) total_days=29
				else if(yy%100==0) total_days=28
				else total_days=28
			break
		};

		for(d=1; d<=total_days; d++){
			for(var x=0; x<obj_content_size; x++){
				var init_o_d = parseInt(obj_content[x][0]),
					init_o_m = parseInt(obj_content[x][1]),
					init_o_y = parseInt(obj_content[x][2]);

				if(init_o_d==d && init_o_m==m && init_o_y==y){
					o_t = obj_content[x][3],
					o_p = obj_content[x][4];
				};
			};

			var str_d = d<=9?'0'+d:d,
				a     = (m==current_month && y==current_full_year && d==current_day) || (m!=current_month && d==1) || (m==start_month && (d==8) && y<current_full_year) ? 'active' : '',
				c     = (m==current_month && d>current_day) || (m>current_month && d>1 && y==current_full_year) || (m==start_month && d<=start_day && y<current_full_year) ? css : '',
				t     = (m==current_month && d>current_day) || (m>current_month && d>1 && y==current_full_year) || (m==start_month && d<=start_day && y<current_full_year) ? tooltip : '';

			o_t = c=='' ? o_t  : 'aguarde...',
			o_p = c=='' ? o_p  : '#';

			html+="<li"+ c +"><a href='"+ o_p +"' title='"+ d +"/"+ m +"/"+ y +" - "+ o_t +"' class='"+ a +"'>"+ str_d +" "+ t +"</a></li>\n";
			pre_html+="<li"+ c +"><a href='"+ o_p +"' title='"+ d +"/"+ m +"/"+ y +" - "+ o_t +"'>"+ str_d +" "+ t +"</a></li>\n";
			pos_html+="<li"+ c +"><a href='"+ o_p +"' title='"+ d +"/"+ m +"/"+ y +" - "+ o_t +"'>"+ str_d +" "+ t +"</a></li>\n";
			
			init_o_d='', init_o_m='', init_o_y='', o_t=''; o_p='';
		};
	
		days_list.html(pre_html + pre_html + html + pos_html + pos_html);

		html     = '';
		pre_html = '';
		pos_html = '';

		days_list.find('li a').unbind('click').click(function(){
			if($(this).parent().hasClass('empty')) return false;
			var p = $(this).attr('href'),
				d = $(this).attr('title').split(' - ')[0],
				t = $(this).attr('title').split(' - ')[1],
				m = d.split('/')[1];

			days_list.find('li a').removeClass('active');
			$(this).addClass('active');

			var d_metric = (d.split('/')[0]<=9?'0'+d.split('/')[0]:d.split('/')[0]),
				m_metric = (str_months[d.split('/')[1]-1]),
				a_metric = d.split('/')[2].substring(2,4),
				d_m      = d_metric + m_metric + a_metric;

			if(after_day==1){ METRICS._stop_video(actual_date, current_time); actual_date = d_m; }else{ after_day = 1; actual_date = d_m; };
			
			LOAD_CONTENTS(p, d, t, d_m);
			EVENTS_FX._slider_day_list();
			METRICS._days(d_m);
			return false;
		});

		days_list.find('li a.active').click();
	}

};

var LOAD_CONTENTS = function(p, d, t, d_m){

	flash_content.fadeTo(300, 0, function(){
		swfobject.embedSWF(p+'?border=0&amp;enablejsapi=1&amp;playerapiid=ytplayer', "flash_content", "454", "274", "8", null, null, params, null);
		setTimeout(function(){
			flash_content.fadeTo(300, 1);
		}, 300);
	});

};

var EVENTS_FX = {

	_slider_day_list: function(){
		var pos_active_month = ($('#months li a.active').offset().left - adjust_position),
			pos_active_day   = $('#days li a.active').offset().left - adjust_position,
			pos_list_days    = $('#days').offset().left - adjust_position,
			resultado        = parseFloat(pos_list_days+(pos_active_month-pos_active_day)),
			res;

		res = /MSIE 8\.0/i.test(navigator.appVersion)==true ? resultado+2 : resultado;

		$("#days").animate({ left: res }, 1000, function(){
			if(cont==0) days_list.css('opacity', 1);
			cont++;
		});
	},

	_slider_hidden_day_list: function(){
		/* -------------------- */
	}

};

var METRICS = {

	_anchors: function(){
		$('.bt_conheca_o_hot_site').click(function(){
			pageTracker._trackEvent('navegacao','click','hotsite_linea');
		});
		$('.bt_fiat').click(function(){
			pageTracker._trackEvent('navegacao','click','logo_fiat');
		});
		$('.bt_tudo_sobre_o_linea').click(function(){
			pageTracker._trackEvent('navegacao','click','monte_seu_linea');
		});
	},
	_days: function(d){
		pageTracker._trackEvent('navegacao','seleciona_data',''+d+'');
	},
	_play_video: function(d, c, g){
		pageTracker._trackEvent('video','play',d);
	},
	_stop_video: function(d, c){
		status_play=0;
		pageTracker._trackEvent('video','stop',d,c);
	},
	_complete_video: function(d, g){
		pageTracker._trackEvent('video','fim',d,g);
	}

};

jQuery(function(){
	PNG_FIX();
	EXTERNAL_LINK();
	METRICS._anchors();
	BUILD_CONTENTS._stored_elements();
	window.onresize = function(){
		adjust_position = ($('html').width()-$('#container').width())/2;
		EVENTS_FX._slider_day_list();
	};
});