var activeCellId = false;
var initialOffset = 0;
$(function() {
	if ($("#calendar").size() > 0) {
		updateCalendar(initialOffset);
	}
	$("select.auto").change(function(){
		$(this).parent("form").submit();
	});
	
	$("a.site").click(function(){
		id = $(this).attr('rel');
		$.ajax({
			data: {section: 'siteClick', action: 'add', productid: id},
			type: "POST",
			url: "ajax.php"
		});
		return true;
	});
	
	$("#addObjectButton").click(function(){
		$("#addObject").html();
		$.get(_appRoot+'/ajax.php', {section:'addObject',action:'add_form'} , function(data){
			$("#addObject").html(data);
			$("form.validate").validate();
			curvyCorners.redraw();
		  gmapInit();
		}, "html");
	});
});
function updateCalendar(offset) {
	$.post(_appRoot+'/ajax.php', {section:'calendar',offset:offset} , function(data){
	$("#calendar").html(data);
	$("#calendar a.nav").click(function() {
			updateCalendar($(this).attr('rel'));
			return false;
		});
	if (activeCellId) {
	} else {
		var date = new Date();
		activeCellId="#cal_"+date.getFullYear()+"_"+(date.getMonth()+1)+"_"+date.getDate();
	}
	$(activeCellId).addClass('active');
	}, "html");
}

function gmapInit( myLatLng, myZoom, myMapType ) {
	if (GBrowserIsCompatible()) {
		if (typeof myLatLng == 'undefined') {
			var myLat = '42.601619944327965';
			var myLng = '25.11474609375';
		} else {
			var myArr = myLatLng.split(',');
			var myLat = myArr[0].replace(/^\s+|\s+$/g, "");
			myLat = myLat.replace(/\(/, "");
			var myLng = myArr[1].replace(/^\s+|\s+$/g, "");
			myLng = myLng.replace(/\)/, "");
		}
		
		if (typeof myZoom == 'undefined') {
			var myZoom = 7;
		}
		
		if (typeof myMapType == 'undefined') {
			var myMapType = 'G_NORMAL_MAP';
		}
	
		var map = new GMap2(document.getElementById("gmap"));
		
		GEvent.addListener(map, "moveend", function() {
			document.getElementById('lngltd').value = map.getCenter();
			document.getElementById('zoom').value = map.getZoom();
			document.getElementById('maptype').value = map.getCurrentMapType().getUrlArg();
		});
		
		map.setCenter(new GLatLng(myLat, myLng), myZoom);
		map.setMapType(myMapType);
		
		map.addControl(new GLargeMapControl());
		var mapControl = new GMapTypeControl();
		map.addControl(mapControl);
	}
}
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	}
	limitCount.value = limitNum - limitField.value.length;
}

// poller
var pollerGraphMaxWidth = 127;
var pollerGraphMinWidth = 18;
var pollerSlideSpeed = 3;
var pollerTotalVotesHeading = 'Общо гласове: ';
var pollerVotes = new Array();
var pollerTotalVotes = new Array();

function pollerVote(pollId, formId) {	
	var option = $("#"+formId+" input[type=radio]:checked");
	if (option.size() > 0) {
		var optionId = option.val();
		pollerPrepare(pollId);
		$.post(_appRoot + '/ajax.php?section=poll&pollId=' + pollId+ '&optionId=' + optionId, {} , function(data){
			pollerRenderResult(pollId,data);
		}, 'json');
	}
}	

function pollerResult(pollId) {
	pollerPrepare(pollId);
	$.post(_appRoot + "/ajax.php" + '?section=poll&pollId=' + pollId, {} , function(data){
		pollerRenderResult(pollId,data);
	}, 'json');
}
function pollerPrepare(pollId) {
	$('#poller_question' + pollId).hide();
	$('#poller_waitMessage' + pollId).show();
}
function pollerRenderResult(pollId, data) {
	$('#poller_waitMessage' + pollId).hide();
	var resultDiv = $('#poller_results' + pollId);
	
	resultDiv.append($("<p>").addClass('pollerTitle_result').html(data.title));
	
	pollerVotes[pollId] = new Array();
	pollerTotalVotes[pollId] = 0;
		
	for (var i = 0; i < data.options.length; i++) {
		var text = data.options[i].text;
		var id = data.options[i].id;
		var votes = data.options[i].votes;
		
		pollerVotes[pollId][id] = votes;
		pollerTotalVotes[pollId] = pollerTotalVotes[pollId] + parseInt(votes);
		
		resultDiv.append($("<p>").addClass('pollerOption_result').html(text));
		
		var voteDiv = $("<div>").addClass('pollerGraph_result');
		resultDiv.append(voteDiv);
		voteDiv.append($("<div>").addClass('pollerGraphLeft_result'));
		voteDiv.append($("<div>").addClass('pollerGraphMid_result').attr('id', 'result_voteTxt' + id).html('0%'));
		voteDiv.append($("<div>").addClass('pollerGraphRight_result'));
	}
	
	voteDiv.append($("<p>").addClass('pollerTotalVotes').html(pollerTotalVotesHeading + pollerTotalVotes[pollId]));
	
	pollerSetPercentage(pollId);
	pollerSlideVotes(pollId,0);
}


function pollerSetPercentage(pollId) {
	var sum = 0;
	for(var item in pollerVotes[pollId]){
		var percent = Math.round((pollerVotes[pollId][item] * 1.0 / pollerTotalVotes[pollId]) * 100);
		pollerVotes[pollId][item] = percent;
		sum += percent;
	}
	pollerVotes[pollId][item] = pollerVotes[pollId][item] + (100 - sum);
}


function pollerSlideVotes(pollId, currentPercent) {
	currentPercent = parseInt(currentPercent) + 2;
	
	for (var item in pollerVotes[pollId]) {
		if (pollerVotes[pollId][item] >= currentPercent) {
			$('#result_voteTxt' + item).html(currentPercent + '%').css('width', Math.max(pollerGraphMinWidth,Math.round(currentPercent/100*pollerGraphMaxWidth)));
		}
	}
	
	if(currentPercent < 100) {
		setTimeout('pollerSlideVotes("' + pollId + '","' + currentPercent + '")',pollerSlideSpeed);
	}
}
