Cufon.replace(".txt, #nav_ul a", { hover: true });

$(document).ready(function()
{
    var datasets = {
    		"Cena �rednia": {
		        label: "Cena �rednia",
		        data: [
		               [(new Date("2010/01/01")).getTime(), 3.65], 
		               [(new Date("2010/01/02")).getTime(), 4.00], 
		               [(new Date("2010/01/03")).getTime(), 4.10], 
		               [(new Date("2010/01/04")).getTime(), 3.80], 
		               [(new Date("2010/01/05")).getTime(), 3.57],
		               [(new Date("2010/01/06")).getTime(), 3.55],
		               [(new Date("2010/01/07")).getTime(), 3.20],
		               [(new Date("2010/01/08")).getTime(), 2.55],
		               [(new Date("2010/01/09")).getTime(), 2.95],
		               [(new Date("2010/01/10")).getTime(), 3.60],
		               [(new Date("2010/01/11")).getTime(), 3.70],
		               [(new Date("2010/01/12")).getTime(), 2.95],
		               [(new Date("2010/01/13")).getTime(), 2.85],
		               [(new Date("2010/01/14")).getTime(), 2.95],
		               [(new Date("2010/01/15")).getTime(), 2.45],
		               [(new Date("2010/01/16")).getTime(), 1.7],
		               [(new Date("2010/01/17")).getTime(), 1.1],
		               [(new Date("2010/01/18")).getTime(), 3.2]
		        ]
		    },
            "Rynek 1": {
                label: "Rynek 1",
                data: [
                       [(new Date("2010/01/01")).getTime(), 4.15], 
                       [(new Date("2010/01/02")).getTime(), 4.20], 
                       [(new Date("2010/01/03")).getTime(), 4.00], 
                       [(new Date("2010/01/04")).getTime(), 3.95], 
                       [(new Date("2010/01/05")).getTime(), 3.87],
                       [(new Date("2010/01/06")).getTime(), 3.95],
                       [(new Date("2010/01/07")).getTime(), 3.95],
                       [(new Date("2010/01/08")).getTime(), 2.95],
                       [(new Date("2010/01/09")).getTime(), 3.45],
                       [(new Date("2010/01/10")).getTime(), 4.15],
                       [(new Date("2010/01/11")).getTime(), 3.95],
                       [(new Date("2010/01/12")).getTime(), 3.25],
                       [(new Date("2010/01/13")).getTime(), 3.45],
                       [(new Date("2010/01/14")).getTime(), 3.55],
                       [(new Date("2010/01/15")).getTime(), 2.95],
                       [(new Date("2010/01/16")).getTime(), 1.95],
                       [(new Date("2010/01/17")).getTime(), 0.95],
                       [(new Date("2010/01/18")).getTime(), 3.95]
                ]
            },
            "Rynek 2": {
                label: "Rynek 2",
                data: [
                       [(new Date("2010/01/01")).getTime(), 3.15], 
                       [(new Date("2010/01/02")).getTime(), 3.80], 
                       [(new Date("2010/01/03")).getTime(), 4.20], 
                       [(new Date("2010/01/04")).getTime(), 3.65], 
                       [(new Date("2010/01/05")).getTime(), 3.27],
                       [(new Date("2010/01/06")).getTime(), 2.95],
                       [(new Date("2010/01/07")).getTime(), 2.45],
                       [(new Date("2010/01/08")).getTime(), 1.95],
                       [(new Date("2010/01/09")).getTime(), 2.45],
                       [(new Date("2010/01/10")).getTime(), 3.05],
                       [(new Date("2010/01/11")).getTime(), 3.45],
                       [(new Date("2010/01/12")).getTime(), 2.55],
                       [(new Date("2010/01/13")).getTime(), 2.15],
                       [(new Date("2010/01/14")).getTime(), 2.35],
                       [(new Date("2010/01/15")).getTime(), 1.95],
                       [(new Date("2010/01/16")).getTime(), 1.45],
                       [(new Date("2010/01/17")).getTime(), 1.25],
                       [(new Date("2010/01/18")).getTime(), 2.45]
                ]
            },
            "Rynek 3": {
                label: "Rynek 3",
                data: [
                       [(new Date("2010/01/01")).getTime(), 3.15],
                       [(new Date("2010/01/02")).getTime(), 3.80],
                       [(new Date("2010/01/03")).getTime(), 4.20],
                       [(new Date("2010/01/04")).getTime(), 3.65],
                       [(new Date("2010/01/05")).getTime(), 3.27],
                       [(new Date("2010/01/06")).getTime(), 2.95],
                       [(new Date("2010/01/07")).getTime(), 2.45],
                       [(new Date("2010/01/08")).getTime(), 1.95],
                       [(new Date("2010/01/09")).getTime(), 2.45],
                       [(new Date("2010/01/10")).getTime(), 3.05],
                       [(new Date("2010/01/11")).getTime(), 3.45],
                       [(new Date("2010/01/12")).getTime(), 2.55],
                       [(new Date("2010/01/13")).getTime(), 2.15],
                       [(new Date("2010/01/14")).getTime(), 2.35],
                       [(new Date("2010/01/15")).getTime(), 1.95],
                       [(new Date("2010/01/16")).getTime(), 1.45],
                       [(new Date("2010/01/17")).getTime(), 1.25],
                       [(new Date("2010/01/18")).getTime(), 2.45]
                ]
            }
            /*,
            "Rynek 3": {
                label: "Rynek 3",
                data: [[1988, 3813], [1989, 3719], [1990, 3722], [1991, 3789], [1992, 3720], [1993, 3730], [1994, 3636], [1995, 3598], [1996, 3610], [1997, 3655], [1998, 3695], [1999, 3673], [2000, 3553], [2001, 3774], [2002, 3728], [2003, 3618], [2004, 3638], [2005, 3467], [2006, 3770]]
            },
            "Rynek 4": {
                label: "Rynek 4",
                data: [[1988, 6402], [1989, 6474], [1990, 6605], [1991, 6209], [1992, 6035], [1993, 6020], [1994, 6000], [1995, 6018], [1996, 3958], [1997, 5780], [1998, 5954], [1999, 6178], [2000, 6411], [2001, 5993], [2002, 5833], [2003, 5791], [2004, 5450], [2005, 5521], [2006, 5271]]
            },
            "Rynek 5": {
                label: "Rynek 5",
                data: [[1988, 4382], [1989, 4498], [1990, 4535], [1991, 4398], [1992, 4766], [1993, 4441], [1994, 4670], [1995, 4217], [1996, 4275], [1997, 4203], [1998, 4482], [1999, 4506], [2000, 4358], [2001, 4385], [2002, 5269], [2003, 5066], [2004, 5194], [2005, 4887], [2006, 4891]]
            }*/
        };

    // hard-code color indices to prevent them from shifting as
    // countries are turned on/off
    var i = 0;
    $.each(datasets, function(key, val) {
        val.color = i;
        ++i;
    });
    
    // insert checkboxes 
    var choiceContainer = $("#choices");
    var loop = 0; //zmiena tymczasowa
    //wyrysuj elementy formularza
    $.each(datasets, function(key, val) {
        if (loop < 2)   {
        choiceContainer.append('<br/><input type="checkbox" name="' + key +
                               '" checked="checked" id="id' + key + '">' +
                               '<label for="id' + key + '">'
                                + val.label + '</label>');
                            ++loop;
        }else   {
            choiceContainer.append('<br/><input type="checkbox" name="' + key +
                               '" id="id' + key + '">' +
                               '<label for="id' + key + '">'
                                + val.label + '</label>');
                            
        }
    });
    choiceContainer.append("<br /><br /><b>Seria z ostatnich : </b><br /><select  id='select-rynek'><option>Ostatni tydzie�</option><option>Ostatni miesi�c</option><option>Ostatni kwarta�</option><option>Ostatni rok</option></select>");
    $('#select-rynek').change(function(){
        location.reload();
    });
    
    var currentTime = new Date()
    var month = currentTime.getMonth() + 1
    var day = currentTime.getDate()
    var year = currentTime.getFullYear();
    
    if (day < 10){day = "0" + day}
    
    if (month <10){month = "0" + month}



    choiceContainer.append('<br /><br /><b>Seria z zakresu :</b><br />Od :  <input  class="data" value="" name="data" class="input" size="15" >');
    choiceContainer.append(' do: <input  class="data" value="'+year+'-'+month+'-'+day+'" name="data" class="input" size="15" >');

    // koniec generowania elementow formularza
    choiceContainer.find("input").click(plotAccordingToChoices);

 //datapicker
    var dateMin = new Date();

	               $(".data").datepicker(
				{
					maxDate: dateMin,
					yearRange: '-90:+0',
					dateFormat: 'yy-mm-dd',
	                      numberOfMonths: 1,
	                      firstDay: 1,
	                      changeFirstDay: false,
	                      monthNames:['Stycze�','Luty','Marzec','Kwiecie�','Maj','Czerwiec','Lipiec','Sierpie�','Wrzesie�','Pa�dziernik','Listopad','Grudzie�'],
	    			dayNamesMin: [ 'Nd','Po', 'Wt', '�r', 'Cz', 'Pt', 'So']
	                });
    //koniec datapickera

    function plotAccordingToChoices() {
        var data = [];

        choiceContainer.find("input:checked").each(function () {
            var key = $(this).attr("name");
            if (key && datasets[key])
                data.push(datasets[key]);
        });

        if (data.length > 0)
            $.plot($("#placeholder"), data, {
        		grid: {
        			backgroundColor: 'white',
        			borderWidth: 0,
        			hoverable: true
        		},
                yaxis: { },
                xaxis: { 
                	mode: "time",
                    minTickSize: [1, "day"],
                    monthNames: ["sty", "lut", "mar", "kwi", "maj", "cze", "lip", "sie", "wrz", "paz", "lis", "gru"]
                },
                series: {
                    lines: { show: true },
                    points: { show: true }
                },
                legend: {
                    show: true
                    //container: '#choices'
                }	
            });
    }
    
    function showTooltip(x, y, contents) {
        $('<div id="tooltip">' + contents + '</div>').css( {
            position: 'absolute',
            display: 'none',
            top: y + 5,
            left: x + 5,
            border: '1px solid #fdd',
            padding: '2px',
            'background-color': '#fee',
            opacity: 0.80
        }).appendTo("body").fadeIn(200);
    }
    
    var previousPoint = null;
    $("#placeholder").bind("plothover", function (event, pos, item) {
        $("#x").text(pos.x.toFixed(2));
        $("#y").text(pos.y.toFixed(2));

        if (item) {
            if (previousPoint != item.datapoint) {
                previousPoint = item.datapoint;
                
                $("#tooltip").remove();
                var x = new Date(item.datapoint[0]),
                    y = item.datapoint[1].toFixed(2);
                	dt = x.getDate() + "." + x.getMonth()+1 + "." + x.getFullYear();
                
                showTooltip(item.pageX, item.pageY,
                            item.series.label + " - " + dt + "- <b>" + y + " z�</b>");
            }
        }
        else {
            $("#tooltip").remove();
            previousPoint = null;            
        }
    });

    plotAccordingToChoices();
   
	/*
	if($("#placeholder").length > 0) {
		$.plot($("#placeholder"), [ [[0, 0], [1, 1]] ], { yaxis: { max: 1 } });
	}
	*/
	$(document).pngFix();
	$('.pkt-label').addClass('dn');
	$('.pkt-n-label').addClass('dn');
	
	$('#czlonkowie-slide').cycle({
		fx:    	'fade',
		pager:  '.rynek-pkt',
   		before: onBefore,
   		after: 	onAfter,
   		timeout: 5000, 
		next:   '#slide-control-next',
		prev:   '#slide-control-prev',
   		pagerAnchorBuilder: function(idx, slide) { return '.rynek-pkt:eq(' + idx + ')'; 	}
	});
	
	function onBefore() {
		var id = $(this).attr('jq_id');
		activate_pkt(id);
	}
	
	function onAfter() {
	}

	$(".rynek-pkt").mouseover(function() {
		$(this).click();
		$('#czlonkowie-slide').cycle('pause');	
	});
	
	$(".rynek-pkt").mouseout(function() { $('#czlonkowie-slide').cycle('resume'); });
	$(".pkt-label").mouseover(function() {	$('#czlonkowie-slide').cycle('pause'); });
	$(".pkt-label").mouseout(function() { $('#czlonkowie-slide').cycle('resume'); });
	$(".pkt-info").mouseover(function() {	$('#czlonkowie-slide').cycle('pause'); });
	$(".pkt-info").mouseout(function() { $('#czlonkowie-slide').cycle('resume'); });
	
	function activate_pkt(id) {
   	   	rynek_pkt = "#pkt-"+id;
   	   	rynek_label = "#pkt-label-" + id;
   	   	rynek_info = "#pkt-info-"+id;
   	   	
   	   	if(!$(rynek_pkt).is('.rynek-active')) {
	   		$(".rynek-pkt").removeClass('rynek-active');
	   		/*$(".rynek-pkt").addClass('rynek-inactive');*/
	   		$(rynek_pkt).addClass('rynek-active');

			$('.pkt-label').fadeOut('slow');
			$(rynek_label).fadeIn('slow');
			adjust_pos(rynek_pkt,rynek_label);
			
			$('.pkt-info').hide();
			$(rynek_info).fadeIn(1000);
   	   	}
		
	}
	
	function adjust_pos(pkt,label) {
		var re = /px/;
		if($(label).css('height').replace(re, "") == 'auto') 
			height = 15;
		else 
			height = $(label).css('height').replace(re, "");
		
		if($(label).css('width').replace(re, "") == 'auto') 
			width = 100;
		else 
			width = $(label).css('width').replace(re, "");
				
		left1 = $(pkt).css('left').replace(re, "") - width + 20;
		top1 = $(pkt).css('top').replace(re, "") - height - 10;
		
		//window.alert( $(label).css('width').replace(re, "")  + " " + left1 + " " + top1);
		$(label).css('left',left1+"px");
		$(label).css('top',top1+"px");
		
	}
	
	var closing_timeout = 0;
	
	$('.pkt-n-label').mouseenter(function() {
		closing_timeout = 0;
	});
	$('.pkt-n-label').mouseleave(function() {
		var id =  "#"+$(this).attr('id');
		close_label(id);
	});
	
	$('.pkt-niezrzeszony').mouseenter(function() {
		
		var id = $(this).attr('jq_id');
		label = "#pkt-n-label-"+id;
		pkt = "#pkt-n-"+id;
		
		/* close all labels */
		if($(label).css('display') != 'block') {
			$('.pkt-n-label').fadeOut();	
			$(label).fadeIn('slow');
			closing_timeout = 0;
			adjust_pos(pkt,label);
		}
		//else closing_timeout = 0;
	});
	
	$('.pkt-niezrzeszony').mouseleave(function() {
		var id = $(this).attr('jq_id');
		label = "#pkt-n-label-"+id;
		close_label(label);
	});
	
	function close_label(label) {
		closing_timeout = 1;
		setTimeout(function(){
			if(closing_timeout == 0) return 0;
			$(label).fadeOut();	
			closing_timeout = 0;
		},1500);
	}
	
	$('.rynek-pkt a').click(function() {
		window.location.href = $(this).attr('href');
	});
	
	
	/* Prezentacje rynkow - zakladki */
	var map;
	var marker;
	
	if($('#map').attr('jq_lat')) {

		var lat = $('#map').attr('jq_lat');
		var lon = $('#map').attr('jq_lon');
		var name = $('#map').attr('jq_name');
		
		load();
	}

	
	$('.rynek-tab').addClass("dn");
	
	$('.dolmenu').click(function(event) {
		id = "#" + $(this).attr("id").replace("tab-", "")
		
		$('.rynek-tab').addClass("dn");
		$(id).removeClass("dn");
		
		$('.dolmenu').removeClass('home_active');
		$(this).addClass('home_active');

		if(id == "#mapa") {
			map.setCenter(new GLatLng(lat, lon), 13);
			map.checkResize();
		}
	});
	
	var myFile = document.location.toString();
	if (myFile.match('#')) { 
	  var myAnchor = '#' + myFile.split('#')[1];
	  $('.dolmenu[href="' + myAnchor + '"]').click();
	} else {
	  $('.dolmenu:first').click();
	}
		
	function load() {
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("map"));
			map.setCenter(new GLatLng(lat, lon), 13);
			map.addControl(new GLargeMapControl);
			point = new GLatLng(lat,lon);
			createMarker(point);
		}
	}

	function createMarker(point) {
			marker = new GMarker(point);
			map.addOverlay(marker);
			
			marker.openInfoWindowHtml(name);
	}
	
	
	/* Prezentacje rynkow - koniec */
	
});