$(document).ready(function() {
	
	var hash = document.location.hash;
	loadHash(hash);
	
/*	$("#lang_box p a").click(function(e){
		document.location = document.location.pathname+"?lang="+$(this).text().toLowerCase()+currentPage;
		e.preventDefault();
	})
*/	

	homepage_banners(1);


	if($.browser.mozilla) {	   
		Cufon.replace('#corporate h2 a', { fontFamily: 'evn', hover: true, textShadow: '#000 2px 2px'});	   
	}

//	Cufon.replace('div.pres_cnt p.subtitle', { fontFamily: 'events', hover: false});
//	Cufon.replace('#article #main_area h2', { fontFamily: 'events', hover: false});

	$('li.picture a').lightBox();


	$('#searchString').attr('value','');

	/* FEATURED HOMEPAGE */
	$('div.featured_item p.next').click(function(){
		$('#featured').animate({left: -790*($('div.featured_item').index($(this).parent())+1)});
	});
	$('div.featured_item p.prev').click(function(){
		$('#featured').animate({left: ($('#featured').position().left +790)});
	});
	$('div.featured_item p.icon').hover(function(){ $(this).addClass('hover'); },function(){ $(this).removeClass('hover'); });
	/* // FEATURED HOMEPAGE */


	/* TWITTER CYCLE  */
	$("#twitter_cnt").scrollable({ 
		size: 1,
		items: '#twitter_update_list',
		interval: 3000,
		vertical: true,
		loop: true
	});
	$('#twitter_div .navi').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});
	/* // TWITTER CYCLE */



	$('#logo').click(function(){
		closePopup();
		$('#featured_cnt #featured').fadeIn();
		return false;
	});



	$('.ajax-link').live('click', function(){
		$('.ajax-link').removeClass('selected');
		$(this).addClass('selected');
		$('#featured_cnt #featured').fadeOut();
		var url = $(this).attr('href');
		if ($('#content').length > 0 )
		{	
			$('#footer').css({'top':'0px'}); 
			$('#main #content').animate({ top: '-1500' }, 1000, 
			function() { 
				$('#main #ajax_content_cnt').empty(); 
//				$(this).addClass('selected');
				myAjaxLoad(url); 
			});
		} else {
//			$(this).addClass('selected');
			myAjaxLoad(url); 
		}
		return false;
	});



	$('.ajax-link2').live('click', function(){
		$('.ajax-link2').removeClass('selected');
		$(this).addClass('selected');
		var url = $(this).attr('href');
		$('#main #content').animate({ top: '10' }, 1000, 
		function() { 
			$('#main #body').empty(); 
			myAjaxLoad2(url); 
		});
		return false;
	});





	$('form').live('submit', function() {
		var current_url = document.location.hash.split('#page=');
		var dataString = $(this).serialize();
		var selector = '#content';
		var new_url = $(this).attr('action');
		if (!new_url || new_url.substr(0,1) == '#')
		{
		} else {
			current_url[1] = new_url;
		}
		setHash('page='+current_url[1]);

		$.ajax({
			type: "POST",  
			url: current_url[1],  
			data: dataString, // param1=value&param2=value 
			complete: function( res, status ) {
				// If successful, inject the HTML into all the matched elements
				//alert('done ' + href + ' status ' + status + res.responseText);
				if ( status === "success" || status === "notmodified" ) {
						
						
						$('#ajax_content_cnt').html( selector ?
						// Create a dummy div to hold the results
						jQuery("<div />")
							// inject the contents of the document in, removing the scripts
							// to avoid any 'Permission Denied' errors in IE
							.append(res.responseText.replace(/<script(.|\s)*?\/script>/gi, ""))
							// Locate the specified elements
							.find(selector) :
						// If not, just inject the full result
						res.responseText );
						$('#main').css({'z-index':'19'}); 
						$('#main #content').animate({ top: '100' }, 1000).animate({ top: '10' }, 500, 
						function() { 
							$curent_section = $('#content').attr('class');
							if ($('#corporate li').hasClass($curent_section))
								$('#corporate li.'+$curent_section).children('a').addClass('selected');
							{
							}
							$('#main #content').css({'position':'relative'}); 
							//$('#footer').css({'top':'-570px'}); 
						});
				} else {
					// server err or 404
				}
		   }  
		 });  
		return false;
	});

});
	// this so we know were we are at all times.. 
	// used for language switch
	var currentPage = "";	
	
	var myAjaxLoad = function(href, data) {		
		var selector = '#content';
		var selector_presentation = '#presentation_silde_cnt p.visual_default';
		var protocol = document.location.protocol;
		var host = document.location.host;
		var pathname = href.replace(protocol + '//' + host,'');
		setHash('page=' + pathname);		
		var link = location.hash.replace('#page=','');			  

		jQuery.ajax({
			url: href,
			data: data || null,
			dataType: "html",
			complete: function( res, status ) {
				// If successful, inject the HTML into all the matched elements
				//alert('done ' + href + ' status ' + status + res.responseText);
				if ( status === "success" || status === "notmodified" ) {
						currentPage = document.location.hash;						
						
				
//						$('#presentation_silde_cnt').html( selector_presentation ? jQuery("<div />").append(res.responseText.replace(/<script(.|\s)*?\/script>/gi, "")).find(selector_presentation) : res.responseText);
						$('#ajax_content_cnt').html( selector ?
						// Create a dummy div to hold the results
						jQuery("<div />")
							// inject the contents of the document in, removing the scripts
							// to avoid any 'Permission Denied' errors in IE
							.append(res.responseText.replace(/<script(.|\s)*?\/script>/gi, ""))
								

							// Locate the specified elements
							.find(selector) :

						// If not, just inject the full result
						res.responseText );
						$('#main').css({'z-index':'19'}); 
						$('#main #content').animate({ top: '100' }, 1000).animate({ top: '10' }, 500, 
						function() { 
							$curent_section = $('#content').attr('class');
							if ($('#corporate li').hasClass($curent_section))
								$('#corporate li.'+$curent_section).children('a').addClass('selected');
							{
							}
							$('#main #content').css({'position':'relative'}); 
							//$('#footer').css({'top':'-570px'}); 
						});
						//console.log(this);
						
						if ($('ul.pictures').length > 0)
						{
							$('li.picture a').lightBox();
						}
						if ($('#map').length > 0)
						{
							startMap();
						}
						
						if($('#substitute_lang').size()){
							$("#lang_box").html($('#substitute_lang').html());
						}
						
				} else {
					// server err or 404
				}
			}
		});
	}


	var setHash = function(item) {
		var hash = item;
		document.location.hash = hash;
	}

	var loadHash = function(hash) {
		var new_hash = hash.split('#page=');
		if (new_hash[1])
		{
			$('#featured').fadeOut();
			myAjaxLoad(new_hash[1]);
		}
	}

	var removeHash = function(param) {
		var hash = '';
		document.location.hash = hash;
	}



	var closePopup = function(){
		$('a').removeClass('selected');
		$('#footer').css({'top':'0px'}); 
		$('#main #content').animate({ top: '-1500' }, 1000, 
		function() { 
			$('#main #ajax_content_cnt').empty(); 
		});
		removeHash('page');		
	}



/* MAP */
function createCustomMarkerImage()
{
	var myImage = new YImage();
	myImage.src = 'http://preview.events.in.greenpixel.ro/_res/img/pin_events.png';
	myImage.size = new YSize(50, 65);
	myImage.offsetSmartWindow = new YCoordPoint(0, 0);
	return myImage;
}

function createSimpleMarker(map, lat, lng, label, icon)
{
	var point = new YGeoPoint(lat, lng);
	var marker = new YMarker(point, createCustomMarkerImage());
//							marker.setSmartWindowColor("black");
//							marker.addAutoExpand(label);
	map.addOverlay(marker);
}

function startMap()
{

	var map = new YMap(document.getElementById('map'));
	var $coords = $('div.boxMap p.coords').text().split(',');
	var $address= $('div.boxMap div.head').text();
	map.addZoomLong();
	map.addPanControl();
	
	map.setMapType(YAHOO_MAP_REG);
	
	var center = new YGeoPoint($coords[0],$coords[1]);
	
	map.drawZoomAndCenter(center,3);

	YEvent.Capture(map, EventsList.MouseClick, reportPosition);
	function reportPosition(_e, _c){
		var mapCoordCenter = map.convertLatLonXY(0,0);
		YLog.initPos(mapCoordCenter); 
		getCoordinates(_c.Lat,_c.Lon);
	}

	createSimpleMarker(map,$coords[0],$coords[1]);

}

function getCoordinates(lat,lon) {
	$('p.get_coords span').text(lat+' , '+lon);
}

/* // MAP */





	var myAjaxLoad2 = function(href, data) {		
		var selector = '#body_cnt';			
		var protocol = document.location.protocol;
		var host = document.location.host;
		var pathname = href.replace(protocol + '//' + host,'');
		setHash('page=' + pathname);		
		var link = location.hash.replace('#page=','');			  

		jQuery.ajax({
			url: href,
			data: data || null,
			dataType: "html",
			complete: function( res, status ) {
				// If successful, inject the HTML into all the matched elements
				//alert('done ' + href + ' status ' + status + res.responseText);
				if ( status === "success" || status === "notmodified" ) {
						currentPage = document.location.hash;
									
						
						$('#body').html( selector ?
						// Create a dummy div to hold the results
						jQuery("<div />")
							// inject the contents of the document in, removing the scripts
							// to avoid any 'Permission Denied' errors in IE
							.append(res.responseText.replace(/<script(.|\s)*?\/script>/gi, ""))
								

							// Locate the specified elements
							.find(selector) :

						// If not, just inject the full result
						res.responseText );
						if ($('ul.pictures').length > 0)
						{
							$('li.picture a').lightBox();
						}
						
				} else {
					// server err or 404
				}
			}
		});
	}

function homepage_banners(x){
	n = $('#presentation_silde_cnt p.visual').length;
	if (n > 1)
	{
		current_banner = '#presentation_silde_cnt p.pos_'+x;
		if (x<=n)
		{
			$('#presentation_silde_cnt p.visual').fadeOut().removeClass('selected');
			$(current_banner).fadeIn().addClass('selected');
			x++;
			$timer = setTimeout( function() { homepage_banners(x);}, 5000);
		} else {
			homepage_banners(1);
		}
	} else {
		$('#presentation_silde_cnt p.pos_1').fadeIn().addClass('selected');
	}
}
