$(document).ready(function() {
	$(".info_tooltip2").bubble2(50, 80);
	$(".info_tooltip").bubble(50, 80);

	if (typeof search_st != "undefined") {
		var search_start = search_st - 1;
	} else {
		var search_start = 0;
	}

	if (typeof search_count != "undefined") {
		if (search_count <= 2) { 
			$("#last_search .box_next img").attr("src", "images/arrow_next_off.gif");
			$("#last_search .box_next img").css({ cursor: "default"});
		}
		if (search_count > 1) {
			if (search_start == search_count-1) {
				var search_start = search_start - 1;
			} else {
				var search_start = search_start;
			}
		}
		if (search_start == 0) {
			$("#last_search .box_prev img").attr("src", "images/arrow_prev_off.gif");
			$("#last_search .box_prev img").css({ cursor: "default"});
		}
	}
	
	if (typeof fittings_count != "undefined") {
		if (fittings_count <= 2) { 
			$("#fittings .box_next img").attr("src", "images/arrow_next_off.gif");
			$("#fittings .box_next img").css({ cursor: "default"});
		}
	}
	$("#fittings .box_prev img").css({ cursor: "default"});
	
	function change_buttons(button_prev, button_next, counter, array) {
		var first = array[0].id.substring(1,(array[0].id.length));
		var end = array[1].id.substring(1,(array[1].id.length));
		if (first > 1) {
			$(button_prev+" img").attr("src", "images/arrow_prev.gif");
			$(button_prev+" img").css({ cursor: "pointer"});
		} else {
			$(button_prev+" img").attr("src", "images/arrow_prev_off.gif");
			$(button_prev+" img").css({ cursor: "default"});
		}					
		if (end < counter) {
			$(button_next+" img").attr("src", "images/arrow_next.gif");
			$(button_next+" img").css({ cursor: "pointer"});
		} else {
			$(button_next+" img").attr("src", "images/arrow_next_off.gif");
			$(button_next+" img").css({ cursor: "default"});
		}
	}
	$("#box_wyszukane").jCarouselLite({
		btnNext: "#last_search .box_next",
		btnPrev: "#last_search .box_prev",
		visible: 2,
		start: search_start,
		easing: "easeinout",
		speed: 1000,
		circular: false,
		afterEnd: function(a) {
			change_buttons('#last_search .box_prev', '#last_search .box_next', search_count, a);
		}
	});

	$("#box_fittings").jCarouselLite({
		btnNext: "#fittings .box_next",
		btnPrev: "#fittings .box_prev",
		visible: 2,
		easing: "easeinout",
		speed: 1000,
		circular: false,
		afterEnd: function(a) {
			change_buttons('#fittings .box_prev', '#fittings .box_next', fittings_count, a);
		}
	});					

});
var firstTime = true;

function getActualSettings() { 
	start_tab = warianty;
	for (var y = 0; y < _max; y++ ){
		var pos2 = $("#cecha"+ y)[0].selectedIndex-1;
		if (start_tab[pos2]['params']) {
			var start_tab = start_tab[pos2]['params'];	
		} else {
			var start_tab = start_tab[pos2]['tab'];	
		}
	}
	aktualizujStroneHTML(start_tab);
}

function createVariants(warianty_local, actOpt, poziom) {
	if (actOpt == null || actOpt.length == 0 || actOpt[poziom] == null) {		//actOpt - jeśli brak to null, jeśli pusta tablica to length = 0
		searchId = warianty_local[0]['id'];
	} else {
		searchId = actOpt[poziom];
	}
	var poziom = (poziom == null) ? 0 : poziom;

	$("#cecha"+poziom).empty().append("<option value=\"\">"+_defaultText+"</option>");

	for (var x = 0; x < warianty_local.length; x++) {
		var tmp_opt = $("<option>").attr({value: warianty_local[x]['id']}).html(warianty_local[x]['name']);
		if (warianty_local[x]['id'] == searchId) {
			tmp_opt.attr("id", "act"+poziom);
			if (warianty_local[x]['tab']) {
				createVariants(warianty_local[x]['tab'], actOpt, (poziom+1));
			}
		}
		$("#cecha"+poziom).append(tmp_opt);
	}
	var selectActive = function() {
		$("#act"+poziom).attr("selected", "selected");
		getActualSettings();
	};
//		setTimeout(selectActive,1);// : selectActive();
}

function updateVariants (selectId) {
	var actual_tab = update_tab = warianty;
	if ($("#cecha"+ selectId)[0].selectedIndex-1 >= 0) {
		if ($("#cecha"+ (selectId-1)).length != 0) {
			for (var x = 0; x < selectId; x++) {
				var pos = $("#cecha"+ x)[0].selectedIndex-1;
				var actual_tab = actual_tab[pos]['tab'];	
			}
			createVariants(actual_tab, null, (selectId+1));
		} else {
			if (actual_tab[$("#cecha"+ selectId)[0].selectedIndex-1]['tab']) {
				createVariants(actual_tab[$("#cecha"+ selectId)[0].selectedIndex-1]['tab'], null, (selectId+1));
			} else {
				aktualizujStroneHTML(actual_tab[$("#cecha"+ selectId)[0].selectedIndex-1]['params']);
			}
		}
	} else {
		for (var y = selectId+1; y < _max; y++ ){
			$("#cecha"+y).empty().append("<option value=\"\">"+_defaultText+"</option>");	
		}
	}
//	id = document.getElementById('cecha0').value;
//	alert(id);
//	document.getElementById('probki_kolorow').child.style.border="none";
//	document.getElementById('kolorek'+id).style.border="1px solid #ff0000";
}
function wybierzSelectZGrafy(wybrany) {
	temptab = [wybrany];
	createVariants(warianty, temptab, 0);
}
function ustawCene(c) {
    if (!window._defaultWyprzedaz) window._defaultWyprzedaz=0;
    w=c-c*(window._defaultWyprzedaz/100);
    return w;
}
function aktualizujStroneHTML(dane) {
	if (dane) {
		if ( dane[2] == 0 ) {
			$("#cena").text(_defaultCena + " zł");
			$("#cena_info").text((Math.round(((100*_defaultCena)/122)*100)/100) + " zł");
        } else {
			$("#cena").text(ustawCene(dane[2]) + " zł");
			$("#cena_info").text((Math.round(((100*ustawCene(dane[2]))/122)*100)/100) + " zł");
        }
        if (dane[3]) {
            $("#prod_grafika").css("background-image","url('"+ _defaultKat + dane[3]+"')");
            $("#prod_grafika2").attr("src", _defaultKat + dane[3]);
			$("#prod_grafika").attr("href", _defaultKat_big + dane[3]);
        } else {
            $("#prod_grafika").css("background-image","url('"+ _defaultGrafika+"')");
            $("#prod_grafika2").attr("src", _defaultGrafika);
            $("#prod_grafika").attr("href", _defaultGrafika_big);
        }
        $("#dostawa").text(dane[5]);
		$("#wart").val(dane);

	} else {
        $("#prod_grafika").css("background-image","url('"+ _defaultGrafika+"')");
        $("#dostawa").text(_defaultDostepnosc);
		$("#wart").val("");
    }
	co = 'wariant';
	ktory = dane[0];
}

function thisMovie(movieName) 
{
	return ($.browser.msie) ? window[movieName] : document[movieName];
}
function sprawdzenieFormy()
{
	if(typeof _max != 'undefined') {
		for (var i = 0; i < _max; i++) {
			if ($("#cecha"+ i)[0].selectedIndex == 0) {
				return false;
			}
		}
	}
    return true;
}
jQuery.fn.bubble = function(top, left) {
	var bubble = null;
	var body = null;
	var _created = false;
    
	return this.each(function() {
		$(this).hover(
			function() {
				if (!_created) {
					$(document.body).append("<div id=\"status-bubble\"><div></div></div>");
					bubble = $("div#status-bubble");
					body = bubble.find("div");
					_created = true;
				}
				var offset = getOffset(this);
				var txt = $(this).next("div.bubble").html();
				//alert($(this).attr("src"));
				body.html(txt);
				h = bubble.get(0).offsetHeight;
				bubble.css({
					top: (offset[1] + top - h + 10) + "px",
					left: (offset[0] + left - 120) + "px",
					visibility: "visible"
				});
			},
			function() {
				bubble.css("visibility", "hidden");
			}
		);
		
	});
};
jQuery.fn.bubble2 = function(top, left) {
	var bubble2 = null;
	var body = null;
	var _created = false;
    
	return this.each(function() {
		$(this).hover(
			function() {
				if (!_created) {
					$(document.body).append("<div id=\"status-bubble2\"><div></div></div>");
					bubble2 = $("div#status-bubble2");
					body = bubble2.find("div");
					_created = true;
				}
				var offset = getOffset(this);
				var txt = $(this).next("div.bubble2").html();
				//alert($(this).attr("src"));
				body.html(txt);
				h = bubble2.get(0).offsetHeight;
				bubble2.css({
					top: (offset[1] + top - h + 10) + "px",
					left: (offset[0] + left - 120) + "px",
					visibility: "visible"
				});
			},
			function() {
				bubble2.css("visibility", "hidden");
			}
		);
		
	});
};
function getOffset(o) {
    var top = 0, left = 0;

	while (o.offsetParent) {
        top += o.offsetTop  || 0;
        left += o.offsetLeft || 0;
        o = o.offsetParent;
    };

	return [left, top];
//	return [o.offsetTop, o.offsetLeft]
}

