var idCount;
var vehIds;

function updateVehIdSession(){
	idCount = 0;
	vehIds = '';
	
	$('.vehId').each(function(){
		if(idCount > 0){
			vehIds += "," + $(this).val();
		}else{
			vehIds += $(this).val();
		}
		idCount++;
	});
	
	$('#vehId').val(vehIds);
	$('#comparrisonHTML').val($('#compare-list-items').html());

	$.post('http://www.apollocamper.co.nz/compare-session.aspx', $('#compare-details').serialize());
}

function isScrolledIntoView(elem){
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
}

$(function(){
	
	if($('#vehId').length > 0){
		var selVehIds = $('#vehId').val();
		if(selVehIds != ""){
			vehIds = selVehIds.split(',');
			
			var x;
			for (x in vehIds){
				$('a#vehicle' + vehIds[x]).addClass('selected-vehicle');
			}
			
			$('.empty-text').hide();
			$('#compare-list-items').append($('#comparrisonHTML').val());
			
			if($('#compare-list-items .compare-item').size() > 1){
				$('#compare-btn').removeClass('disabled');
			}
		}
	}
	
	$(window).scroll(function(){
		if(isScrolledIntoView('#side-search')){
			$('.scrollIntoView').removeClass('scrollIntoView');
		}else{
			if(selVehIds != ""){
				var docWidth = $(window).width();
				var siteWidth = 980;
				var rightOffset = ((docWidth - siteWidth) / 2) + 11;
				//console.log(docWidth + " - " + siteWidth + " / 2 = " + rightOffset);
				$('#compare-list-wrapper').addClass('scrollIntoView').css({right: rightOffset});
			}
		}
	});
	
	$('a.add-vehicle').click(function(e){
		var vehDetail = $(this).attr('rel');
		vehDetail = vehDetail.split(','); // vehDetail[0] = vehicle ID, vehDetail[1] = vehicle name, vehDetail[2] = vehicle subtitle
		
		if(!isScrolledIntoView('#side-search')){
			var docWidth = $(window).width();
			var siteWidth = 980;
			var rightOffset = ((docWidth - siteWidth) / 2) + 11;
			//console.log(docWidth + " - " + siteWidth + " / 2 = " + rightOffset);
			$('#compare-list-wrapper').addClass('scrollIntoView').css({right: rightOffset});
		}else{
			$('.scrollIntoView').removeClass('scrollIntoView');
		}
		
		$('.empty-text').hide();
		
		if($(this).is('.selected-vehicle')){
			$('.vehicle' + vehDetail[0]).slideUp(100, function(){
				$(this).remove();
				updateVehIdSession();
			});
			
			if($('#compare-list-items .compare-item').size() == 2){
				setTimeout("$('#compare-btn').addClass('disabled')", 100);
			}
			if($('#compare-list-items .compare-item').size() == 1){
				setTimeout("$('.empty-text').show();", 100);
			}
			
			$(this).removeClass('selected-vehicle');
			return false;
		}else{
			if($('#compare-list-items .compare-item').size() == 1){
				$('#compare-btn').removeClass('disabled');
			}
		}
		
		var string = "<div class='compare-item vehicle" + vehDetail[0] + "'><div class='inner'>";
		string += "<a href='#' class='remove-item' rel='" + vehDetail[0] + "' title='Remove this vehicle from the list'>remove</a>";
		string += "<img src='/graphics/compare/thumb-" + vehDetail[0] + ".png' class='compare-item-img' /><div class='compare-item-details'><div class='vehicle-name'>";
		string += vehDetail[1];
		string += "</div>";
		if(vehDetail[2]){
			string += "<div class='vehicle-subtitle'>" + vehDetail[2] + "</div>";
		}
		string += "<input type='hidden' name='compare-id' class='vehId' value='" + vehDetail[0] + "' />";
		string += "<div class='clear'></div></div>";
		
		$('#compare-list-items').append(string);
		
		$(this).addClass('selected-vehicle');
		
		updateVehIdSession();
		
		e.preventDefault();
	});
	
	$('a.remove-item').live('click', function(e){
		var vehId = $(this).attr('rel');
		$('#vehicle' + vehId).removeClass('selected-vehicle');
		
		$(this).parents('.compare-item').slideUp(100, function(){
			$(this).remove();
			updateVehIdSession();
		});
		
		if($('#compare-list-items .compare-item').size() == 2){
			setTimeout("$('#compare-btn').addClass('disabled')", 100);
		}
		if($('#compare-list-items .compare-item').size() == 1){
			setTimeout("$('.empty-text').show();", 100);
		}
		
		e.preventDefault();
	});
	
	$('#compare-btn').click(function(){
		if($(this).is('.disabled')){
			alert("Please select more than one vehicle to compare");
			return false;
		}
		
		var count = 0;
		var idString = '';
		var langId = 1;
		if($(this).attr('rel') > 1){
			langId = $(this).attr('rel');
		}
		
		$('.vehId').each(function(){
			if(count > 0){
				idString += "&v=" + $(this).val();
			}else{
				idString += "v=" + $(this).val();
			}
			count++;
		});
		
		var colWidth = ($('#compare-list-items .compare-item').size() * 322) + (($('#compare-list-items .compare-item').size() - 1) * 10) + 60;

		var width = $(window).width() - 20;
		var height = $(window).height() - 100;
		
		if(colWidth < width){
			width = colWidth;
		}
		
		$.colorbox({
			href:    "http://www.apollocamper.co.nz/campervan-hire-vehicles-compare.aspx?" + idString + "&l=" + langId,
			iframe:     true,
			title:      "Compare Vehicles",
			height:     height,
			innerWidth: width
		});
	});
	
	$('a.shadowbox').colorbox({
			href:    $(this).attr('href'),
			iframe:     true,
			title:      $(this).attr('title'),
			height:     450,
			width:      600
	});
	
	$('#showthrough').colorbox({
		iframe:true,
		innerWidth:640,
		innerHeight:580
	});
});
