var HOST = 'http://www.itas-dannunzio.it'
var BASEURL = '';
var IMGFOLDER = 'public';

var map;

$(document).ready(function() {

	if($('#big-image .picture').size() > 0) {
		$('#big-image .picture').cycle({ 
			fx: 'fade', 
			speed: 4000 
		});
	}
	
	$('#archive select').change(function() {
		if($(this).val() != '') {
			document.location.href = $(this).val();
		}
	}).val('articoli.php' + document.location.href.substr(document.location.href.indexOf('?')));
	
	$('#archive input[name="r"]').val('Cerca sul sito').blur(function() {
		if($.trim($(this).val()) == '') {
			$(this).val('Cerca sul sito').css('color', '#999');
		}
	}).focus(function() {
		if($.trim($(this).val()) == 'Cerca sul sito') {
			$(this).val('').css('color', '#333');
		}
	});
	
	$('#blog .post-list input[name="r"]').val('Cerca nelle circolari').blur(function() {
		if($.trim($(this).val()) == '') {
			$(this).val('Cerca nelle circolari').css('color', '#999');
		}
	}).focus(function() {
		if($.trim($(this).val()) == 'Cerca nelle circolari') {
			$(this).val('').css('color', '#333');
		}
	});
	
	if($('#map').size() > 0) {
		$('#map').css({
			'width': '380px',
			'height': '220px',
			'background-color': '#999'
		});
		
		map = new GMap2(document.getElementById("map"));
		//map.addControl(new GMapTypeControl());
		map.addControl(new GSmallMapControl());
		map.setCenter(new GLatLng(45.9483, 13.6238), 16);
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
		
		var gpoint = new GLatLng(45.9483, 13.6238);
		var mymarker = createMarker(gpoint, 'red');
		map.addOverlay(mymarker);		
	}
	
	function createMarker(point, color) {
		icon = createIconMarker(color);
		var marker = new GMarker(point, icon);
		GEvent.addListener(marker, "click", function() {
			alert('test');
		});
		return marker;
	}

	function createIconMarker(arg) {
		var baseicon = new GIcon();
		baseicon.image = 'images/marker_34_' + arg + '.png';
		baseicon.shadow = "images/shadow50.png";
		baseicon.iconSize = new GSize(20, 34);
		baseicon.shadowSize = new GSize(37, 34);
		baseicon.iconAnchor = new GPoint(9, 34);
		baseicon.infoWindowAnchor = new GPoint(9, 2);
		baseicon.infoShadowAnchor = new GPoint(18, 25);
		return baseicon;
	}
	
});

/* COOKIES */

function getCookie(check_name) {
	var a_all_cookies = document.cookie.split(';');
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false;

	for (i = 0; i < a_all_cookies.length; i++) {
		a_temp_cookie = a_all_cookies[i].split('=');
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		if (cookie_name == check_name) {
			b_cookie_found = true;
			if (a_temp_cookie.length > 1)
				cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if (!b_cookie_found)
		return null;
}

function setCookie(name, value, expires, path, domain, secure) {
	var today = new Date();
	today.setTime(today.getTime());
	if (expires)
		expires = expires * 1000 * 60 * 60 * 24;
	var expires_date = new Date(today.getTime() + (expires));
	document.cookie = name + '=' +escape(value) +
		((expires) ? ';expires=' + expires_date.toGMTString() : '') +
		((path) ? ';path=' + path : '') +
		((domain) ? ';domain=' + domain : '') +
		((secure) ? ';secure' : '');
}

function deleteCookie(name, path, domain) {
	if(getCookie(name)) document.cookie = name + "=" +
		((path) ? ";path=" + path : "") +
		((domain) ? ";domain=" + domain : "") +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

/* TINY */

bTextareaWasTinyfied = false;

function setTextareaToTinyMCE(sEditorID) {
	try {
		var oEditor = document.getElementById(sEditorID);
		if(oEditor) {
			tinyMCE.execCommand('mceAddControl', false, sEditorID);
			bTextareaWasTinyfied = true;
		}
		return;
	}
	catch(err) { alert('setTextareaToTinyMCE: ' + err); }
}

function unsetTextareaToTinyMCE(sEditorID) {
	try {
		var oEditor = document.getElementById(sEditorID);
		if(oEditor && bTextareaWasTinyfied) {
			tinyMCE.execCommand('mceFocus', false, sEditorID);
			tinyMCE.execCommand('mceRemoveControl', false, sEditorID);
			bTextareaWasTinyfied = false;
		}
		return;
	}
	catch(err) { alert('unsetTextareaToTinyMCE: ' + err); }
}

if(typeof(tinyMCE) != 'undefined') {
	tinyMCE.init({
		theme: "advanced",
		mode: "exact",
		auto_reset_designmode: true,
		theme_advanced_layout_manager: "SimpleLayout",
		theme_advanced_buttons1: "bold,italic,underline,strikethrough,separator,myimage,youtube,separator,link,unlink,separator,undo,redo,separator,removeformat,code",
		theme_advanced_buttons2: "",
		theme_advanced_buttons3: "",
		extended_valid_elements: "script[charset|defer|language|src|type],object[width|height],param[name|value],embed[width|height|src|type|allowscriptaccess|allowfullscreen]",
		theme_advanced_toolbar_location : "top",
		setup : function(ed) {
			// Add a custom button
			ed.addButton('myimage', {
				title : 'Upload an image',
				image : 'images/myimage.gif',
				onclick : function() {
					showUploader(ed);
				}
			});
			ed.addButton('youtube', {
				title : 'Embed a video',
				image : 'images/youtube.gif',
				onclick : function() {
					showEmbedder(ed);
				}
			});
		}
	});
}

/* UTILITIES */

function fixArticle(html) {
	var h = $('<div>' + html + '</div>');
	h.find('p').removeAttr('style').removeAttr('class');
	h.find('span').each(function(i) {
		$(this).replaceWith($(this).text())
	});
	h.find('img').removeAttr('style').removeAttr('class');
	h.find('img').closest('p').attr('style', 'text-align: center;');
	h.find('object').closest('p').attr('style', 'text-align: center;');
	h.find('p').each(function(i) {
		var hh = $(this).html();
		if(($.trim(hh) == '') || ($.trim(hh) == '&nbsp;')) {
			$(this).remove();
		}
	});
	var newhtml = h.html();
	return newhtml;
}

function getURLParam(strParamName, url) {
	var strReturn = "";
	var strHref = url ? url : window.location.href;
	if(strHref.indexOf("?") > -1){
		var strQueryString = strHref.substr(strHref.indexOf("?"));
		var aQueryString = strQueryString.split("&");
		for (var iParam = 0; iParam < aQueryString.length; iParam++){
			if(aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	return unescape(strReturn);
}














