/// <reference path="jquery-1.4.1-vsdoc.js" />

//SCROLL START
function LoadScroller(divId) {
	$('#' + divId).jScrollPane({ maintainPosition:false, showArrows: true, scrollbarWidth: 18, arrowSize: 18, dragMinHeight: 70, dragMaxHeight: 70 });
}
	
function LoadControlScroller() {
	$('#SmallControlScroller').jScrollPane({ showArrows: true, scrollbarWidth: 18, arrowSize: 18, dragMinHeight: 70, dragMaxHeight: 70 });
}
//SCROLL END

//Aftercare START
function AftercareClick() {	
	$.ajax({
		cache: false,
		type: "GET",
		url: "assets/xml/aftercare.xml",
		dataType: "xml",
		success: function (xml) {
			$("#RegionsSelect li").remove();
			
			var _title = $(xml).find("page").find("title").text();
			var _description = $(xml).find("page").find("description").text();

			$("#AftercareTitle").html(_title);
			$("#AftercareDescription").html(_description);
		}
	});

	$.ajax({
		cache: false,
		type: "GET",
		url: "assets/xml/aftercareRegions.xml",
		dataType: "xml",
		success: function (xml) {
			$("#RegionsSelect option:not(:first)").remove();
			$(xml).find("regions").find("region").each(function () {
				var _regionTitle = $(this).attr("title");
				var _regionValue = $(this).attr("value");

				$("#RegionsSelect").append($("<option></option>").val(_regionValue).html(_regionTitle));

				$("#AftercareContainer").modal({ closeTitle: "Close", onOpen: function (dialog) {
					dialog.overlay.fadeIn(300, function () {
						dialog.data.hide();
						dialog.container.fadeIn(300, function () {
							//dialog.data.show();

							dialog.data.fadeIn(800);

							dialog.data.slideDown(600, "easeInQuad");

							LoadScroller("items");
							LoadControlScroller();
						});
					});
				}
				});
			});
		}
	});
}

function AftercareGOClick() {
	$("#SpecialistsList li").remove();

	var _selectedValue = $("#RegionsSelect").val();

	if (_selectedValue == 0) {
		return false;
	}

	$("#AftercareDescription").hide();

	$.ajax({
		cache: false,
		type: "GET",
		url: "assets/xml/aftercareRegions.xml",
		dataType: "xml",
		success: function (xml) {
			$(xml).find("regions").find("region").each(function () {
				if ($(this).attr("value") == _selectedValue) {
					var _regionTitle = $(this).attr("title");

					$("#RegionName").html(_regionTitle);
				}
			});
		}
	});

	$.ajax({
		type: "GET",
		url: "assets/xml/aftercareSpecialists.xml",
		dataType: "xml",
		success: function (xml) {
			var _selectedRegionValue = $("#RegionsSelect").val();

			var _regionName = "";
			var _regionLocation = "";
			var _specialistPhoneNr = "";

			var _listItem = "";

			$("#AftercareDescription").hide();

			$(xml).find("specialists").find("region").each(function () {
				if ($(this).attr("value") == _selectedRegionValue) {
					$(this).find("specialist").each(function () {
						_regionName = $(this).attr("name");
						_regionLocation = $(this).find("location").text();
						_specialistPhoneNr = $(this).attr("phoneNr");

						var _regionNameDiv = $("<div style=\"width: 200px; float:right;\"></div>").html(_regionName);

						var _regionLocationDiv = $("<div style=\"width: 170px; float:right;\"></div>").html(_regionLocation);

						var _specialistPhoneNrDiv = $("<div style=\"width: 160px; float:right;\"></div>").html(_specialistPhoneNr);

						_listItem = $("<div></div>").append(_regionNameDiv).append(_regionLocationDiv).append(_specialistPhoneNrDiv);

						$("#SpecialistsList").append($("<li style=\"width: 530px;\"></li>").append(_specialistPhoneNrDiv).append(_regionLocationDiv).append(_regionNameDiv));

						LoadScroller("items");
						LoadControlScroller();
					});

					return false;
				}
			});
		}
	});
}

function AftercarePrintClick() {
	var _header= $("#AftercareContainer .Header").clone().removeClass("FloatLeft");
	//$("img",_header).attr("src", "assets/images/popup/logoPrint.gif");
	$("img", _header).attr("src", "assets/images/popup/logo.gif");
	
	var _title = $("#AftercareTitle").clone().css("clear","both");
	var _description = $("#AftercareDescription").clone();
	var _region = "<br /><div><b class=\"FontStyle\">Region : </b><span style=\"font-family:Verdana; font-size:14px;\">" + $("#RegionName").text() + "</span></div><br />";
	var _content = $("<div style=\"width:577px;padding-left:15px; margin-top:10px;overflow:hidden; clear:both;\"></div>").append(_title);
	//if ($(_description).length > 0)
	        $(_content).append($(_description).css("display","block").css("color","#000000"));
	if ($("#RegionName").text().length > 0) $(_content).append(_region);
	
	//alert($("#items").html());
	
	//var it= $("#items").clone();
	
	//$( it ).css("position", "relative");
	//$( it ).css("clear", "both");
	//$( it ).find("li").css("color", "#000000");
	$(_content).append($("#items").html()); 
	var _container  = $("<div></div>").append(_header);
	
	$(_content).css("color", "#000000");
	$(_container).append(_content);
	
	$(_container).printElement({ pageTitle: 'Condici Aftercare' });
}


//Aftercare END

//Tips START
function TipsClick() {
	$("#PageLinksList li").remove();

	$.ajax({
		cache: false,
		type: "GET",
		url: "assets/xml/tips.xml",
		dataType: "xml",
		success: function (xml) {
			$(xml).find("pages").find("page").each(function (index) {
				if (index + 1 == 1) {
					$("#PageLinksList").append($("<li class='FloatLeft'></li>").html("<a class=\"FloatLeft PageButtonSelected\" onclick=\"javascript:getTip(this, '" + (index + 1) + "');\">Page " + (index + 1) + "</a>"));
				}
				else {
					$("#PageLinksList").append($("<li class='FloatLeft'></li>").html("<a class=\"FloatLeft PageButton\" onclick=\"javascript:getTip(this, '" + (index + 1) + "');\">Page " + (index + 1) + "</a>"));
				}
			});

			getTip($("#PageLinksList a.PageButtonSelected"), 1);

			$("#TipsContainer").modal({ closeTitle: "Close", onOpen: function (dialog) {
				dialog.overlay.fadeIn(300, function () {
					dialog.data.hide();
					dialog.container.fadeIn(300, function () {
						//dialog.data.show();

						dialog.data.fadeIn(800);

						//dialog.data.slideDown(600, "easeInQuad");

						LoadScroller("itemsTips");
						LoadControlScroller();
					});
				});
			}
			});
		}
	});
}

function getTip(button, _selectedTipValue) {
	$("#PageLinksList a").removeClass("PageButtonSelected").addClass("PageButton");
	$("#SelectedPageHidden").val(_selectedTipValue);
	$(button).addClass("PageButtonSelected");

	$.ajax({
		cache: false,
		type: "GET",
		url: "assets/xml/tips.xml",
		dataType: "xml",
		success: function (xml) {
			var _title = "";
			var _subtitle = "";
			var _description = "";
			var _tip = "";
			var _imageUrl = "";

			//Remove existing tips elements
			$("#TipsElements li[class!='DontRemove']").remove();

			$(xml).find("pages").find("page").each(function () {
				if ($(this).attr("value") == _selectedTipValue) {
					_title = $(this).find("title").attr("value");
					_subtitle = $(this).find("subtitle").attr("value");

					$("#Title").html(_title);
					$("#Subtitle").html(_subtitle);

					//Get all tips elements of a page
					$(this).find("element").each(function () {
						_description = $(this).find("description").text();
						_tip = $(this).find("tip").text();
						_imageUrl = $(this).find("imageUrl").attr("value");
//style=\"display:inline-block;\"
// style=\"display:inline-block; vertical-align:top;\"
						var _img = $("<img id=\"picture\"width=\"226\"  src=\"" + _imageUrl + "\" alt=\"\"  class=\"TipImage FloatLeft\"/>");

						var _tipDiv = "<span class=\"TipDiv FloatLeft\">" + _tip + "</span>";

						var _descriptionDiv = $("<span class=\"TipDescription FontStyle FloatLeft\"></span>").append(_tipDiv).append(_description);
						
						var _div = $("<div style=\"width:1px; height:1px; display:block; clear:both; \"></div>")
						
						//var _div = $("<span class=\"FloatLeft\" style=\"width: 524px;\"></span>").append(_img).append(_descriptionDiv);

						var _listItem = $("<li class=\"\" style=\" width: 524px; page-break-after: always; page-break-inside: avoid; clear:both; display:block;\"></li>").append(_img).append(_descriptionDiv).append(_div);


						$("#TipsElements").append(_listItem);
					});

					//Reload scroll
					LoadScroller("itemsTips");
					LoadControlScroller();

					return false;
				}
			});
		}
	});
}

function TipsPrintClick() {
	var container = $("<div></div>").append($("#TipsContainer .Header").clone());
	//$("img", container).attr("src", "assets/images/popup/topPrint.gif");
	$("img", container).attr("src", "assets/images/popup/top.gif");
	var list = $("<ul style=\"padding:0 0 0 15px; margin:0;\" class=\"ListStyleReset\"></ul>");	
	
	var pageIndex = $("#SelectedPageHidden").val();
	
	$.ajax({
	    cache: false,
	    type: "GET",
	    url: "assets/xml/tips.xml",
	    dataType: "xml",
	    success: function (xml) {
	        var _title = "";
	        var _subtitle = "";
	        var _description = "";
	        var _tip = "";
	        var _imageUrl = "";
	        var _li = "";

	        $(xml).find("pages").find("page").each(function () {
	            if ($(this).attr("value") == pageIndex) {
	                _title = $(this).find("title").attr("value");
	                _subtitle = $(this).find("subtitle").attr("value");

	                _li = $("<li></li>");

	                var titleAux = $("<div style=\"margin:20px 0;\" class=\"TipTitle FontStyle\"></div>").html(_title);
	                var subtitleAux = $("<div class=\"TipSubtitle FontStyle\"></div>").html(_subtitle);

	                $(_li).append(titleAux).append(subtitleAux);

	                $(list).append(_li);


	                var index = 0;
	                //Get all tips elements of a page
	                $(this).find("element").each(function () {
	                    _height = $("#TipsElements li").eq(index).height();

	                    if (navigator.userAgent.toString().indexOf("Chrome") > -1)
	                        _height = 750;

	                    _description = $(this).find("description").text();
	                    _tip = $(this).find("tip").text();
	                    _imageUrl = $(this).find("imageUrl").attr("value");

						var _img = $("<img width=\"226\" src=\"" + _imageUrl + "\" alt=\"\" class=\"TipImage FloatLeft\"/>");

						var _tipDiv = "<span class=\"TipDiv FloatLeft\">" + _tip + "</span>";

						var _descriptionDiv = $("<p class=\"TipDescriptionPrint FloatLeft FontStyle\"></p>").append(_tipDiv).append(_description);

						var _listItem = $("<li style=\"height:" + _height + "px; width: 524px; padding-top:10px; page-break-after: always; page-break-inside: avoid;\"></li>").append(_img).append(_descriptionDiv);

	                    $(list).append(_listItem);
	                });
	            }
	        });

	        $(container).append(list);
	        //$("#itemsTips").append(container);
	        $(container).printElement({ pageTitle: 'Condici Tips' });
	    }
	});	
}
//Tips END

// Map BEGIN

var map = null;

function ShowGoogleMap(latitude, longitude, text) {
	$("#MapContainer").modal({ closeTitle: "Close", onOpen: function (dialog) {
		dialog.overlay.fadeIn(300, function () {
			dialog.data.hide();
			dialog.container.fadeIn(300, function () {

				dialog.data.fadeIn(800);

				loadMap(latitude, longitude, text);
			});
		});
	}
	});
}

function loadMap(latitude, longitude, text) {
	$(document).ready(function () {
		map = new google.maps.Map2(document.getElementById("GoogleMap"));
		geocoder = new GClientGeocoder();

		map.setUIToDefault();

		// Set the maps center
		map.setCenter(new GLatLng(41.899919, 12.479159), 2);

		var point_aux = new GLatLng(latitude, longitude);

		createMarker(point_aux, text);

		map.setCenter(point_aux, 13);
	});
}

//Create a new marker
function createMarker(point, labelText) {
	var markerLabel = labelText;

	var canClick = false;

	if (markerLabel !== '' && markerLabel !== "undefined")
		canClick = true;

	var marker = new GMarker(point, { draggable: false, clickable: canClick });

	// Add events to the marker
	GEvent.addListener(marker, "click", function () {
		marker.openInfoWindowHtml(markerLabel);
	});

	map.addOverlay(marker);
	return marker;
}

// Map END
