//funkcja ustawiania szerokości kontenera list
function setListWidth(container,widthType){
	if (widthType == "paddingBorder") {
		var elementWidth = $(container + " li").outerWidth();
	}
	else if(widthType == "fullWidth"){
		var elementWidth = $(container + " li").outerWidth(true);
	}
	else if(widthType == "normal"){
		var elementWidth = $(container + " li").width();
	}
	else{
		var elementWidth = $(container + " li").width();
	}
	
	var listSum = $(container+" li").size();
	var reelWidth = elementWidth * listSum;
	
	$(container).css({'width' : reelWidth});
}

//funkcja zaznaczania kolorów
function colorSelect(container){
	$(container+" li a").click(function(){
		if ($(this).hasClass('selected')) {
			$(container).find('a').removeClass('selected');
		}
		else {
			$(container).find('a').animate(400,function(){$(this).removeClass('selected')});
			$(this).addClass('selected');
		}
	});
}

//funkcja zaznaczania elementów
function selectMe(container){
	$(container+" li a").click(function(){
		if(jQuery(this).hasClass('selected')){
			$(this).removeClass('selected')
		}
		else{
			$(this).addClass('selected');
		}
	});
}

//funkcja do zazaczania jednego elementu
function selectOnlyMe(container){
	$(container+" li a").click(function(){
		if ($(this).hasClass('selected')) {
			$(container).find('a').removeClass('selected');
		}
		else {
			$(container).find('a').animate(400,function(){$(this).removeClass('selected')});
			$(this).addClass('selected');
		}
	});
}

//funkcja pobierająca rozmiary okna przeglądarki
function _getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ 
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { 
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { 
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { 
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

//funkcja ustawiającą rozmiary pokrycia okna
function setOverlaySize(selector){
	var arrPageSizes = _getPageSize();
	$(selector).css({
		width:				arrPageSizes[0],
		height:				arrPageSizes[1]
	});
	
	$(window).resize(function(){
		var arrPageSizes = _getPageSize();
		$(selector).css({
			width:		arrPageSizes[0],
			height:		arrPageSizes[1]
		});
		setPopupPosition(selector);
	});
}

//funkcja ustawiająca pozycję okienka popup w oknie przeglądarki
function setPopupPosition(selector, next){
    if(!next){
        next = ".techData";
    }
	var arrPageSizes = _getPageSize();
	popupBox = selector+' '+next;
	
	paddingTop = parseInt($(popupBox).css('padding-top'));
	paddingLeft = parseInt($(popupBox).css('padding-left'));
	positionY = arrPageSizes[3]/2 - ($(popupBox).height() + paddingTop)/2  + $(window).scrollTop();
	positionX = arrPageSizes[2]/2 - ($(popupBox).width() + paddingLeft)/2  + $(window).scrollLeft();
	
	$(popupBox).css(
		{
			top: positionY,
			left: positionX
		}
	);
}

//funkcja pokazująca okienko po kliknieciu linku "dane techniczne"
function showTechData(selector){
	$(selector).css('visibility','visible')
}

//funkcja zamykająca okienko
function closeTechData(selector){
	$(selector).css('visibility','hidden')
}

//funkcja pokazująca okienko po kliknieciu przycisku "i" w wersjach
function showPopupData(buttonId,popupName){
	var position = $(buttonId).offset();
	var popupHeight = $(popupName).outerHeight(true);
	var popupWidth = $(popupName).outerWidth(true);
	var leftPosition = position.left - popupWidth/2 - 65;
	var topPosition = position.top - popupHeight;

	$(popupName).css({
		left: leftPosition,
		top: topPosition + 20,
		opacity:0,
		visibility: 'visible'
	})
	
	$(popupName).stop().animate({
		opacity:1,
		top: topPosition
	},200)
}

function closeData(popupName){
	var position = $(popupName).offset();
	
	$(popupName).stop().animate({
		top: position.top + 20,
		opacity:0,
		//visibility: 'hidden'
	},200, function(){
		$(popupName).css('visibility','hidden');
	})
}

function showInterior(buttonId,popupName){
    var position = $("#"+buttonId).offset();
    var popupHeight = $(popupName).outerHeight(true);
    var popupWidth = $(popupName).outerWidth(true);
    var buttonWidth = $("#"+buttonId).outerWidth(true);
    var leftPosition = position.left - popupWidth/2 + buttonWidth/2;
    var topPosition = position.top - popupHeight;
    
    $(popupName).css({
        left: leftPosition,
        top: topPosition - 20,
        opacity:0,
        visibility: 'visible'
    })
    
    $(popupName).stop().animate({
        opacity:1,
        top: topPosition - 20
    },200)
}

function ucfirst (str) {
    var f = str.charAt(0).toUpperCase();
    return f + str.substr(1);
}

$(document).ready(function(){
	setListWidth(".carAdditions","normal");
	//setListWidth(".carBody .colors","fullWidth");
	
	selectMe(".carAdditions");
	
	selectOnlyMe(".carModels");		
	selectOnlyMe(".carEngines");
	selectOnlyMe(".carVersions");
	selectOnlyMe(".carRims");
	
	colorSelect("#bodyColors");		
	colorSelect("#interiorColors");
	
	$('.tablesClipper').jScrollPane({
		verticalDragMaxHeight: 30
	});		

	$("#closeTechData").live("click", function(){
		closeTechData(".overlay");
	});
	
	$(".overlayDiler .closeButton").live("click", function(){
		closeTechData(".overlayDiler");
	});
	
	$(".overlayCalculator .closeButton").live("click", function(){
        closeTechData(".overlayCalculator");
    });
	
	$('#bodyColors a img').live('mouseover', function() {
	    $('.carBody .colorDescription span').text(ucfirst($(this).attr('title')));
	});

            
	$("#closeVersionData").click(function(){
		closeData($(this).parent());
	});
	
	$(".closeData").click(function(){
		closeData($(this).parent());
	})
	
});

