jQuery.extend({
	random: function(X) {
		return Math.floor(X * (Math.random() % 1));
	},
	randomBetween: function(MinV, MaxV) {
		return MinV + jQuery.random(MaxV - MinV + 1);
	}
});

var globalURL = 'http://dacia.ch';

var i = 0;
var activeItem = 0;
var animRunning = false;
var newsOverlayVisible = false;

function handleControls() {
	$('div#news_navigation-prev').hide();
	$('div#news_navigation-next').hide();
	
	if (activeItem == 0) {
		$('div#news_navigation-prev').hide();
		$('div#news_navigation-next').show();
	} else if (activeItem == ($('#slides > .sliderItem').length - 1)) {
		$('div#news_navigation-prev').show();
		$('div#news_navigation-next').hide();
	} else {
		$('div#news_navigation-prev').show();
		$('div#news_navigation-next').show();
	}
}
function slide() {
	if (!animRunning) {
		var slideValue;
		slideValue = activeItem * $('#news_slider').width();
		animRunning = true;
		$('#slides').animate({
			left: (-1 * slideValue) + 'px'
		}, 300, function() {
			animRunning = false;
		});
	}
}
function toggleOverlay() {
	if (!animRunning) {
		if (newsOverlayVisible) {
			// $('#opojan2012').show();
			$('#newsOverlay').fadeOut(300);
			$('#whiteOverlay').fadeOut(300);
			newsOverlayVisible = false;
		} else {
			// $('#opojan2012').hide();
			$('#newsOverlay').fadeIn(300);
			$('#whiteOverlay').fadeIn(300);
			newsOverlayVisible = true;
		}
	}
}
function rearrangeImages() {
	$('#slides .sliderItem').each(function() {
		$(this).find('img').css({
			'top': '50%',
			'left': '50%',
			'margin-left': -($(this).find('img').attr('width') / 2)+'px',
			'margin-top': -($(this).find('img').attr('height') / 2)+'px'
		});
	});
}

function checkVisitState() {

}

$(document).ready(function() {
	handleControls();
	
	$('#slides').css({
		'width': (($('#slides > .sliderItem').length) * $('#news_slider').width()) + 'px'
	});
	
	rearrangeImages();
	
	$('div#news_navigation-prev').click(function() {
		activeItem--;
		if (activeItem == 0) {
			activeItem = 0;
		}
		handleControls();
		slide();
	});
	$('div#news_navigation-next').click(function() {
		activeItem++;
		if (activeItem >= $('#slides > .sliderItem').length) {
			activeItem = $('#slides > .sliderItem').length - 1;
		}
		handleControls();
		slide();
	});
	$('div#news_close-button, div#newsOverlayClicker, div#whiteOverlay').click(function() {
		toggleOverlay();
	});
	
	if ($.browser.msie) {
		$('#backgroundOverlay').removeClass('backgroundOverlay').addClass('backgroundOverlay_ie');
	}
	
	$('div.hover24').hover(function() {
		$(this).find('img').css({
			'margin-top': '-24px'
		});
	}, function() {
		$(this).find('img').css({
			'margin-top': '0px'
		});
	});
	$('div.hover38').hover(function() {
		$(this).find('img').css({
			'margin-top': '-38px'
		});
	}, function() {
		$(this).find('img').css({
			'margin-top': '0px'
		});
	});
	$('div.hover50').hover(function() {
		$(this).find('img').css({
			'margin-top': '-50px'
		});
	}, function() {
		$(this).find('img').css({
			'margin-top': '0px'
		});
	});
	$('div.hover55').hover(function() {
		$(this).find('img').css({
			'margin-top': '-55px'
		});
	}, function() {
		$(this).find('img').css({
			'margin-top': '0px'
		});
	});
	
	$('#modell_duster-city').click(function() { window.location.href = 'dacia_duster_city.php'; });
	$('#modell_duster-4x4').click(function() { window.location.href = 'dacia_duster.php'; });
	$('#modell_sandero').click(function() { window.location.href = 'dacia_sandero.php'; });
	$('#modell_sandero-stepway').click(function() { window.location.href = 'dacia_sandero_stepway.php'; });
	$('#modell_logan').click(function() { window.location.href = 'dacia_logan.php'; });
	$('#modell_logan-mcv').click(function() { window.location.href = 'dacia_logan_mcv_kombi.php'; });
	$('#modell_logan-van').click(function() { window.location.href = 'dacia_logan_van.php'; });
	$('#modell_logan-pickup').click(function() { window.location.href = 'dacia_logan_pickup.php'; });
	
	
	$('area[href="dacia_duster_city.php"]').hover(function() {
        $('#modell_duster-city').find('img').css({'margin-top': '-24px'});
	}, function() {
	    $('#modell_duster-city').find('img').css({'margin-top': '0px'});
	});
	$('area[href="dacia_duster.php"]').hover(function() {
        $('#modell_duster-4x4').find('img').css({'margin-top': '-24px'});
	}, function() {
	    $('#modell_duster-4x4').find('img').css({'margin-top': '0px'});
	});
	$('area[href="dacia_sandero.php"]').hover(function() {
        $('#modell_sandero').find('img').css({'margin-top': '-38px'});
	}, function() {
	    $('#modell_sandero').find('img').css({'margin-top': '0px'});
	});
	$('area[href="dacia_sandero_stepway.php"]').hover(function() {
        $('#modell_sandero-stepway').find('img').css({'margin-top': '-38px'});
	}, function() {
	    $('#modell_sandero-stepway').find('img').css({'margin-top': '0px'});
	});
	$('area[href="dacia_logan.php"]').hover(function() {
        $('#modell_logan').find('img').css({'margin-top': '-38px'});
	}, function() {
	    $('#modell_logan').find('img').css({'margin-top': '0px'});
	});
	$('area[href="dacia_logan_mcv_kombi.php"]').hover(function() {
        $('#modell_logan-mcv').find('img').css({'margin-top': '-38px'});
	}, function() {
	    $('#modell_logan-mcv').find('img').css({'margin-top': '0px'});
	});
	$('area[href="dacia_logan_van.php"]').hover(function() {
        $('#modell_logan-van').find('img').css({'margin-top': '-50px'});
	}, function() {
	    $('#modell_logan-van').find('img').css({'margin-top': '0px'});
	});
	$('area[href="dacia_logan_pickup.php"]').hover(function() {
        $('#modell_logan-pickup').find('img').css({'margin-top': '-50px'});
	}, function() {
	    $('#modell_logan-pickup').find('img').css({'margin-top': '0px'});
	});
	
	// fake links
	$('#fakeNavigation div.linkAreas div').bind('click', function() {
		linktarget = 'home.php';
	  
		switch($(this).attr('name')) {
			case 'newsletter':
				linktarget = 'http://newsletter.renault.ch/u/register.php?CID=121282725&f=22312';
				window.open(linktarget);
				break;
			case 'rechtshinweise':
				linktarget = 'rechtshinweise.php';
				break;
			case 'haendlersuchen':
				linktarget = 'haendlersuche.php';
				break;
			case 'haendlersuche':
				linktarget = 'haendlersuche.php';
				break;
			case 'fr':
				linktarget = 'http://dacia.ch/fr/index.php';
				break;
			case 'it':
				linktarget = 'http://dacia.ch/it/index.php';
				break;
			case 'logo':
				linktarget = 'home.php';
				break;
			case 'start':
				linktarget = 'home.php';
				break;
			case 'sandero':
				linktarget = 'dacia_sandero.php';
				break;
			case 'car_sandero':
				linktarget = 'dacia_sandero.php';
				break;
			case 'sandero-stepway':
				linktarget = 'dacia_sandero_stepway.php';
				break;
			case 'car_sandero-stepway':
				linktarget = 'dacia_sandero_stepway.php';
				break;
			case 'logan':
				linktarget = 'dacia_logan.php';
				break;
			case 'car_logan':
				linktarget = 'dacia_logan.php';
				break;
			case 'logan-mcv':
				linktarget = 'dacia_logan_mcv_kombi.php';
				break;
			case 'car_logan-mcv':
				linktarget = 'dacia_logan_mcv_kombi.php';
				break;
			case 'duster-4x4':
				linktarget = 'dacia_duster.php';
				break;
			case 'car_duster-4x4':
				linktarget = 'dacia_duster.php';
				break;
			case 'duster-city':
				linktarget = 'dacia_duster_city.php';
				break;
			case 'car_duster-city-gross':
				linktarget = 'dacia_duster_city.php';
				break;
			case 'car_duster-city-grossbild':
				linktarget = 'dacia_duster_city.php';
				break;
			case 'car_duster-city':
				linktarget = 'dacia_duster_city.php';
				break;
			case 'logan-van':
				linktarget = 'dacia_logan_van.php';
				break;
			case 'car_logan-van':
				linktarget = 'dacia_logan_van.php';
				break;
			case 'logan-pickup':
				linktarget = 'dacia_logan_pickup.php';
				break;
			case 'car_logan-pickup':
				linktarget = 'dacia_logan_pickup.php';
				break;
			case 'inderpresse':
				linktarget = 'presse.php';
				break;
			case 'mehrerfahren':
				linktarget = 'form.php';
				break;
			case 'broschuere':
				linktarget = 'form.php?check_broschuere=2';
				break;
			case 'finanzierung':
				linktarget = 'form.php?check_finanzierung=1';
				break;
			case 'probefahren':
				linktarget = 'form.php?check_probefahrt=1';
				break;
			case 'kundenzufriedenheit':
				linktarget = 'aktuell.php';
				break;
			case 'autosdiemansichleistenkann':
				linktarget = 'aktuell.php';
				break;
			case 'dassagendaciafahrer':
				linktarget = 'aktuell.php';
				break;
				
		}
	  
		if (($(this).attr('name') != 'status-demo-ch' && $(this).attr('name') != 'newsletter')) {
			window.location.href = linktarget;
		}
	}).hover(function() {
		switch($(this).attr('name')) {
			case 'start':
				imageChange('menu1','img_menu1_akt');
				break;
			case 'sandero':
				imageChange('menu2','img_menu9_akt');
				break;
			case 'sandero-stepway':
				imageChange('menu3','img_menu12_akt');
				break;
			case 'logan':
				imageChange('menu4','img_menu2_akt');
				break;
			case 'logan-mcv':
				imageChange('menu5','img_menu8_akt');
				break;
			case 'duster-4x4':
				imageChange('menu6','img_menu13_akt');
				break;
			case 'duster-city':
				imageChange('menu7','img_menu14_akt');
				break;
			case 'logan-van':
				imageChange('menu8','img_menu10_akt');
				break;
			case 'logan-pickup':
				imageChange('menu9','img_menu11_akt');
				break;
			case 'inderpresse':
				imageChange('menu10','img_menu3_akt');
				break;
			case 'car_duster-city-gross':
			case 'car_duster-city-grossbild':
				$('#modell-duster-city-gross').find('img').css({
					'margin-top': '-55px'
				});
				break;
			case 'car_duster-city':
				$('#modell_duster-city').find('img').css({
					'margin-top': '-24px'
				});
				break;
			case 'car_duster-4x4':
				$('#modell_duster-4x4').find('img').css({
					'margin-top': '-24px'
				});
				break;
			case 'car_sandero':
				$('#modell_sandero').find('img').css({
					'margin-top': '-38px'
				});
				break;
			case 'car_sandero-stepway':
				$('#modell_sandero-stepway').find('img').css({
					'margin-top': '-38px'
				});
				break;
			case 'car_logan':
				$('#modell_logan').find('img').css({
					'margin-top': '-38px'
				});
				break;
			case 'car_logan-mcv':
				$('#modell_logan-mcv').find('img').css({
					'margin-top': '-38px'
				});
				break;
			case 'car_logan-van':
				$('#modell_logan-van').find('img').css({
					'margin-top': '-50px'
				});
				break;
			case 'car_logan-pickup':
				$('#modell_logan-pickup').find('img').css({
					'margin-top': '-50px'
				});
				break;
		}
	}, function() {
		switch($(this).attr('name')) {
			case 'start':
				imageChange('menu1','img_menu1');
				break;
			case 'sandero':
				imageChange('menu2','img_menu9');
				break;
			case 'sandero-stepway':
				imageChange('menu3','img_menu12');
				break;
			case 'logan':
				imageChange('menu4','img_menu2');
				break;
			case 'logan-mcv':
				imageChange('menu5','img_menu8');
				break;
			case 'duster-4x4':
				imageChange('menu6','img_menu13');
				break;
			case 'duster-city':
				imageChange('menu7','img_menu14');
				break;
			case 'logan-van':
				imageChange('menu8','img_menu10');
				break;
			case 'logan-pickup':
				imageChange('menu9','img_menu11');
				break;
			case 'inderpresse':
				imageChange('menu10','img_menu3');
				break;
			case 'car_duster-city-gross':
			case 'car_duster-city-grossbild':
				$('#modell-duster-city-gross').find('img').css({
					'margin-top': '0px'
				});
				break;
			case 'car_duster-city':
				$('#modell_duster-city').find('img').css({
					'margin-top': '0px'
				});
				break;
			case 'car_duster-4x4':
				$('#modell_duster-4x4').find('img').css({
					'margin-top': '0px'
				});
				break;
			case 'car_sandero':
				$('#modell_sandero').find('img').css({
					'margin-top': '0px'
				});
				break;
			case 'car_sandero-stepway':
				$('#modell_sandero-stepway').find('img').css({
					'margin-top': '0px'
				});
				break;
			case 'car_logan':
				$('#modell_logan').find('img').css({
					'margin-top': '0px'
				});
				break;
			case 'car_logan-mcv':
				$('#modell_logan-mcv').find('img').css({
					'margin-top': '0px'
				});
				break;
			case 'car_logan-van':
				$('#modell_logan-van').find('img').css({
					'margin-top': '0px'
				});
				break;
			case 'car_logan-pickup':
				$('#modell_logan-pickup').find('img').css({
					'margin-top': '0px'
				});
				break;
		}
	});
});

$(window).ready(function() {
	
});

