// JavaScript Document
window.onload = function (){
	fixImages();
};

$(document).ready(function(){
	analyzePath();
	getCurrentBrand();
	hoverLogos();
	starLoad();
	starClick();
	starCount();
});

/*
Adding margins to center images vertically
 */
function fixImages() {
	$.each($('div.img-view div.big img'), function(){
		var imgHeight = $(this).height();
		$(this).css('margin-top', (324-imgHeight)/2);
	});
}

function analyzePath() {
	var path = window.location.pathname;
	path = path.split('/');
	var name = path[1];

	if(name == ''){
		eraseCookie('brand');
	}

	if(path[1] == 'brand'){
		createCookie('brand', path[2], 1);
		name = '';
	} else if (path[1] == 'search') {
		name = 'search/results'
	}

	setupMenu(name);
}

function setupMenu(name) {
	$('li.menu').removeClass('active');
	$('li.menu a[href="/' + name + '"]').parent().addClass('active');
}

function hoverLogos() {
	$('div#brends li.active a:not(.colored)').hover(
	  function () {
		$(this).removeClass("bw");
	  },
	  function () {
		$(this).addClass("bw");
	  }
	);
}

function getCurrentBrand() {
	$('div#brends li.active a').click(function(){
		var brand = $(this).attr('id');
		createCookie('brand', brand, 1);
	});

	var brand = readCookie('brand');

	if(brand){
		changeBrand(brand);
	}
}

function changeBrand(brand) {
	$('div#brends li.active a').addClass("bw").removeClass("colored");
	$('div#brends li.active a.' + brand).removeClass("bw").addClass("colored");
	$('div.home-bottom-logo img').attr('src', '/img/' + brand + '-logo.png');

	var brandsObj = {'fiat' : 1, 'lancia' : 2, 'alfaromeo' : 3, 'jeep' : 4};
	$('select[name="brand"] option').removeAttr('selected');
	$('select[name="brand"] option[value="' + brandsObj[brand] + '"]').attr('selected', 'selected');
}

function starCount() {
	var wishlist = readCookie('wishlist');
	if(wishlist){
		wishlist = wishlist.split('%2C');
		var number = wishlist.length - 1;
		if(number > 0){
			$('li.favorites a').addClass('active');
			$('li.favorites a').text(number);
		}
	}
}

function starClick() {
	$('a.wishlist').live('click', function(){
		var auto_id = $(this).attr('aid');
		$.ajax({
			type: "POST",
			url: "/favorite/ajax",
			data: {'auto_id': auto_id},
			dataType : "json",
			success: $.proxy(function(data){
				var number = $('li.favorites a').text();
				if(data == 1){
					number++;
					$('span', this).addClass('active');
					$('li.favorites a').text(number);
					if(number > 0) $('li.favorites a').addClass('active');
				} else {
					number--;
					$('span', this).removeClass('active');
					$('li.favorites a').text(number);
					if(number == 0) $('li.favorites a').removeClass('active');
				}
			}, this)
		});
		return false;
	});
}

function starLoad() {
	var wishlist = readCookie('wishlist');
	if(wishlist){
		$('a.wishlist').each(function(){
			var auto_id = $(this).attr('aid');
			var check = wishlist.indexOf(auto_id);
			if(check != -1){
				$('span', this).addClass('active');
			}
		});
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toUTCString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
