var limit = 5;

$(document).ready(function() {	
	if ($('#mapContainer').length > 0 ) {		
		loadMap("mapContainer",14.5053864,46.0507184);
	} 
	
	if ($('#mapContainer_w').length > 0 ) {
		
		loadMap("mapContainer_w",14.5053864,46.0507184)
		getLocations(limit,0);
		animateController("first");
	}
	
	if($('#coordinates').length >= 1) {
		showAddress($('#coordinates').attr("value"));
	}
	
	$('#address').blur(function() {
		moveToAddress($('#address').attr("value"),$('#number').attr("value"));
	});
	
	$('#number').blur(function() {
		moveToAddress($('#address').attr("value"),$('#number').attr("value"));
	});
	
	$('#mapInfo dt').click(function() {
		customToggle ();
	});	
	
	$('#mapController li').click(function() {
		animateController (this.id);
	});		
});	

function customToggle () {
	if($('#mapInfo').hasClass('sel')) {
		if ($('#third').hasClass('sel')) {
			hideInfo('dd_timeline');
		} else {
			hideInfo('dd_info');
		}
	} else {
		if ($('#third').hasClass('sel')) {
			showInfo('dd_timeline');
		} else {
			showInfo('dd_info');
		}
	}
}

function loadLocationData (id,show) {
	$.post("?_wms_module=GOOGLE_MAP::getLocationData", {'id':id},function(data){
		var batch = data.split("|");
		var emptySrc = 'http://'+window.location.hostname+'/uploads/contrib/empty.gif';
		var src;
		if (batch[1].length == 52) {src = 'http://'+window.location.hostname+'/'+batch[1]} else {src = emptySrc}
		$('.image_c #cam').attr("src", src);
		if (batch[2].length == 52) {src = 'http://'+window.location.hostname+'/'+batch[2]} else {src = emptySrc}		
		$('.image_c #see').attr("src", src);
		
		
		$('#cam_data').html('<div class="sel_hidden" id="'+batch[0]+'" style="display:none; visibility:hidden; height:0px;"></div><p>'+
		'<strong>Kje:</strong><br/>'
		+batch[3]+ '' +batch[4] + '<br/>'
		+ '1000 Ljubljana<br/><br/>'
		+'<strong>Kdo je prispeval:</strong><br/>'
		+batch[5]+ '<br/><br/>'
		+'<strong>Kdaj je prispeval:</strong><br/>'
		+batch[6] 
		+'</p>');
		
		$('#cam_data_info').html('<p>'+
		'<strong>Informacije:</strong><br/>'
		+batch[7]+'</p>');
				
		if($('#mapInfo #dd_info').hasClass('hidden') && show == true) {
			showInfo();
		}		
	});
}

function animateController (item) {
	var items = ["first", "second", "third"];
	var sel;
	var clicked = item.id;
	var selWidth;
	var innerSelWidth;
	var firstWidth;
	
	for ( var i in items ) { 
		var id = items[i];
		if ($('#'+id).hasClass('sel')) {
			sel = id;
			break;
		}
	}
	
	if(!$('#'+item).hasClass('sel')){
		if(!sel) {
			$('#'+item).addClass('sel');
			$('#'+item+ ' .cont_c').animate({width:"460px"}, {duration:320});
			$('#'+item+ ' .center').animate({width:"440px"}, {duration:320});
			getPagging(0,limit,item);
		}  else {
			$('#'+sel).css({float:"left"});
			$('#'+sel+ ' .center').animate({width:"194px"}, {duration:320});
			$('#'+sel+ ' .cont_c').animate({width:"214px"}, {duration:320});
			$('#'+sel+ ' .cont_c').queue(function() {
				$('#'+sel).removeClass('sel');
				$('#'+sel + ' .center').css({width:"214px"});
				
				$('#'+item).queue(function() {
					$('#'+item + ' .center').css({width:"194px"});
					$('#'+item).addClass('sel');
					$('#'+item+ ' .cont_c').animate({width:"460px"}, {duration:320});
					$('#'+item+ ' .center').animate({width:"440px"}, {duration:320});
										
					$('#'+item+ ' .center').queue(function() {
						if (item == "third") {
							$('#third').css({float:"right"});
						} else {
							$('#third').css({float:"left"});
						}
						$('#'+item+ ' .center').dequeue();
					});
					
					if (item == "first")  {
						hideInfo();
						hideInfo("dd_timeline");
						getLocations (limit,parseInt($('.from_p_c').attr("id")));
					}
					if (item == "second")  {
						hideInfo();
						hideInfo("dd_timeline");
						getLocations("*",0);
					}
					
					if (item == "third")  {
						hideInfo();
						map.clearOverlays ();
						$('#mapInfo').queue(function () {
							getLocations("*",0);
							
							showInfo("dd_timeline");
							//$('#dd_timeline').addClass('sel');
							getMonths();
							$('#mapInfo').dequeue();
						});
					}			
					
										
					$('#'+item).dequeue();
				});
				
				$('#'+sel + ' .cont_c').dequeue();
			});
		}
	}
}

function hideInfo (item) {
	var id;
	if (item) {id = item} else {id="dd_info"}
	$('#mapInfo dt').css({backgroundImage:'url(../dsg/bgr/arrow_u.png)'});
	$('#mapInfo').animate({height:"15px"}, {duration:400});	
	$('#mapInfo').queue(function () {
		$('#mapInfo #'+id).addClass("hidden");
		$('#mapInfo').dequeue();
    });	
	$('#mapInfo').removeClass('sel');
}

function showInfo (item) {
	var id;
	if (item) {id = item} else {id="dd_info"}
	$('#mapInfo dt').css({backgroundImage:'url(../dsg/bgr/arrow_d.png)'});
	$('#mapInfo').queue(function () {
		$('#mapInfo').animate({height:"210px"}, {duration:400});	
		$('#mapInfo #'+id).removeClass("hidden");
		$('#mapInfo').dequeue();
	});
	$('#mapInfo').addClass('sel');
}


function moveToAddress (address,number) {
	if (address.length >= 2) {
		showAddress(address + " " + number + ", ljubljana, slovenia");
	}
}

function outputCoordinates (point) {
	var x = Math.round(point.x * 100000000)/100000000 ;
	var y = Math.round(point.y * 100000000)/100000000 ;
	$("#coordinates").attr({value: y + ", " + x});
}

function mapGoto (x,y) {
	map.setCenter(new GLatLng(y, x), 13);
}

function getPagging (from,limit,item) {
	
	var end = from + limit;
	var start = from + 1;
	var prev = from - limit;
	var startImg = "";
	var endImg = "";
		
	if (start != 1) {startImg='<img onclick="getLocations('+limit+','+prev+');getPagging('+prev+','+limit+',\''+item+'\')" src="http://'+window.location.hostname+'/dsg/bgr/arrow_l.png" alt=""/>'}
	
	$.post("?_wms_module=GOOGLE_MAP::getPagging", {'from':from, 'limit': limit, 'end': end},function(data){
		var info = data.split(",");
		if (info[1] > 0) {endImg='<img onclick="getLocations('+limit+','+end+');getPagging('+end+','+limit+',\''+item+'\')" src="http://'+window.location.hostname+'/dsg/bgr/arrow_r.png" alt=""/>'}
		if(end > info[0]) {end = info[0]} 
		$('#'+item+' .center_info').html('<table cellpadding="0" cellspacing="0">' + 
				'<tr>'+
				'	<td>'+startImg+'</td>'+
				'	<td><p id="'+from+'" class="from_p_c">'+start+'-'+end+' / '+info[0]+'</p></td>'+
				'	<td>'+endImg+'</td>'+
				'</tr>'+
			'</table>');
	});
	
	
}

function getLocations (limit,from, marked) {
	var northEast = map.getBounds().getNorthEast();
	var southWest = map.getBounds().getSouthWest();
	
	var height = northEast.lat() - southWest.lat();
	var width = northEast.lng() - southWest.lng();
		
	var sw = escape(new GLatLng(southWest.lat() - height, southWest.lng() - width));
	var ne = escape(new GLatLng(northEast.lat() + height, northEast.lng() + width));
	
	//alert(limit + '\n'+ from +  '\n' + ne + '\n'+ sw);
	
	$.post("?_wms_module=GOOGLE_MAP::getLocations", {'northEast': ne, 'southWest': sw, 'limit': limit, 'from': from},function(data){
		if ($('#third').hasClass('sel')) {
			
			var locations = data.split("|");
			var count = 0;
			var curDate;
			
			$.timer(700, function (timer) {
				if(locations[0]) {
					if ($('#third').hasClass('sel')) {
						var location = locations[0].split(",");					
						
						if (curDate == location[3]) {
							if(count == 9) {count = 0} else {count++;}
						} else {
							curDate = location[3];
							count = 0;
						}
												
						setupMarker(location,count);										
						locations.shift();
		    			timer.reset(700);
		    			
		    		}
				} else {
					timer.stop();
				}								
			});
				
			
		} else {
			setupMarkers(data,marked);
		}
	});
}

function getMonths () {
	$.post("?_wms_module=GOOGLE_MAP::getMonths", {'id':'id'},function(data){
		
		var dates = data.split("|");
		var first = dates[0].split(',');
		var last = dates[1].split(',');
		var fYear = parseInt(first[0]);
		var fMonth = first[1];
		var lYear = parseInt(last[0]);
		var lMonth = last[1];
				
		
		if (fMonth.substring(0,1) == 0) {
			fMonth = fMonth.substring(1,2);
		}
		
		if (lMonth.substring(0,1) == 0) {
			lMonth = lMonth.substring(1,2);
		}			
		
		var curYear = parseInt(fYear);
		var curMonth = parseInt(fMonth);
		var stop = false;
		
		var doLoop = true;
		$('#td_tower').html("");
		$('#td_month').html("");
		
		
		while (doLoop)  {	
						
			$('#td_tower').append('<td style="vertical-align: bottom"><div id="tower_'+curMonth+'_'+curYear+'" style="width:12px; height:0px;"></div></td>');
			$('#td_month').append('<td align="center"><div style="width:12px; height:10px; margin-right:5px;">'+curMonth+'</div></td>');
			
			if (curYear == lYear) {
				if (curMonth == lMonth) {
					doLoop = false;
				}
			}
			
			if (curMonth == 12) {
				curMonth = 1;
				curYear = curYear + 1;	
			} else {
				curMonth = curMonth + 1
			} 		
		}
		
		
	});
}


