$(document).ready(function(){
	// stuff

Cufon.replace('#presentation .title', { fontFamily: 'ct_font', textShadow: '#666 1px 1px'});
Cufon.replace('div.page_title h1', { fontFamily: 'ct_font'});
Cufon.replace('div.service h2', { fontFamily: 'ct_font'});
Cufon.replace('body.concerte div.body h2', { fontFamily: 'ct_font'});

	/* TWITTER CYCLE  */
	$("#twitter_cnt").scrollable({ 
		size: 1,
		items: '#twitter_update_list',
		next: '#twitter_div .next', 
		prev: '#twitter_div .prev',
		interval: 3000,
		vertical: true,
		loop: true
	});
	$('#twitter_div .navi').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});
	/* // TWITTER CYCLE */



	/* SEARCH INPUT TOP */
	$('#search_key').attr('value','cauta o oferta!');
	$('#search_key').focus(function(){ if ($('#search_key').attr('value') == 'cauta o oferta!') {$('#search_key').attr('value','');} });
	$('#search_key').blur(function(){ if ($('#search_key').attr('value') == '') {$('#search_key').attr('value','cauta o oferta!');} });
	/* // SEARCH INPUT TOP */


	$('ul.boxed li').hover(
		function(){ $(this).addClass('hover'); }, 
		function(){ $(this).removeClass('hover'); }
	);


	/* FEATURED */
	$('#featured p.pagination').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});
	$("#featured_container").scrollable({ 
		size: 3,
		items: 'div.boxes',
		next: '#featured .next', 
		prev: '#featured .prev',
		interval: 4000,
		vertical: false,
		loop: true
	});
	/* // FEATURED */



	/* GALLERY */
	$("div.gallery").scrollable({ 
		size: 3,
		items: 'ul.gallery',
		next: '#gallery_container .next', 
		prev: '#gallery_container .prev',
		interval: 4000,
		vertical: false,
		loop: true
	});

	$('ul.gallery li a').lightBox();
	/*
	if ($('ul.gallery li').length <= 3)
	{
		$('#gallery_container p.prev').addClass('no_prev');
		$('#gallery_container p.next').addClass('no_next');
	}
	*/
	/* // GALLERY */


	/* MENU */
	$('body.homepage ul.main_menu li.first').addClass('current');
	$('ul.main_menu li.selected ul').addClass('hover');
	$('body.homepage ul.main_menu').hover(
		function () {
		},
		function () {
			$('body.homepage ul.main_menu li.first').addClass('current');
		});

	$('ul.main_menu li').hover(
		function () {
			if (!$(this).parent().hasClass('submenu'))
			{
				$('ul.main_menu li').removeClass('current');
				$(this).addClass('current');
				if ($(this).hasClass('selected'))
				{
				} else {
					$('ul.main_menu li.selected ul').removeClass('hover').css('display','none');
					$(this).children('ul').addClass('hover');
				}
			}
		},
		function () {
			if (!$(this).parent().hasClass('submenu'))
			{
				$(this).removeClass('current');
				$(this).children('ul').removeClass('hover');
				$('ul.main_menu li.selected ul').addClass('hover').css('display','block');
			}
		});
	/* // MENU */



	/* BOOK NOW */
	$('p.rezerva_acum').click(function(){
		 $("div.form_container").fadeIn();
		 document.location.hash = '#primary';
//		 return false;
	});
	$('div.form_container #form_book_now div.errors').parent().parent().addClass('with_erors');
	/* // BOOK NOW */


	$('div.boxes div.box:last').addClass('last');


	if ($('body').hasClass('products'))
	{
		$('div.form_container').append('<p class="close">inchide</p>');
		$('div.form_container p.close').live('click', function() {
			$('div.form_container').fadeOut('500');
		});
	}


	/* SECTION TABS */
	if ($('body').hasClass('revelion'))
	{
		counter = 0;
		//circuite(1);
		$('#sections_tabs ul li:last').addClass('last');
		$item_max_position = $('#sections_tabs ul li').length;
		$('#page_sections div').each(function(z){
			$item_position = z+1;
			if ($item_position == 1)
			{
				$(this).append('<div class="days_nav"><p class="next">ziua urmatoare</p></div><p class="close">inchide</p>');
			} else if ($item_position == $item_max_position)
			{
				$(this).append('<div class="days_nav"><p class="prev last">ziua precedenta</p></div><p class="close">inchide</p>');
			} else {
				$(this).append('<div class="days_nav"><p class="prev">ziua precedenta</p><p class="next">ziua urmatoare</p></div><p class="close">inchide</p>');
			}
		});

		$('#page_sections p.close').live('click', function() {
			$('#page_sections div').fadeOut('500');
			$('#sections_tabs').removeClass('cat_selected');
			$('#sections_tabs ul li a').removeClass('selected');
			$('#page_sections p.close').css('display','none');
		});


		$('#body_sections').addClass('expanded');
		$('#body_sections').append('<p class="view_all">programul desfasurat</p>');

		$('#body_sections p.view_all').live('click', function() {
			$('#body_sections').addClass('expanded');
			$(this).text('inchide programul');
			$(this).addClass('close_all');
		});

		$('#body_sections p.close_all').live('click', function() {
			$('#body_sections').removeClass('expanded');
			$(this).text('programul desfasurat');
			$(this).removeClass('close_all');
		});


		$('#sections_tabs ul li').click(function(){
			if ($('#body_sections').hasClass('expanded'))
			{
			} else {
				$('#sections_tabs ul li a').removeClass('selected');
				$('#page_sections div').removeClass('current').css('display','none');
				$('#sections_tabs').addClass('cat_selected');
				$('#page_sections div.days_nav').css('display','block');
				$('#page_sections p.close').css('display','block');
				var index = $('#sections_tabs ul li').index(this) + 1;
				$(this).children().children().addClass('selected');
				$('#page_sections div.pos_'+index).addClass('current').css('display','block');
				return false;
			}
		});


		$('#sections_tabs ul li').hoverIntent({
			over: mouseOver,
			out: mouseOut,
			sensitivity: 3, 
			interval: 200, 
			timeout: 200
		});




		$('div.days_nav p.next').live('click', function() {
			$(this).parent().parent().fadeOut('500');
			$(this).parent().parent().next('div.section').fadeIn('1000');
			$item_position = $(this).parent().parent().next('div.section').attr('class').substr(-1,1);
			$('#sections_tabs ul li a').removeClass('selected');
			$('#sections_tabs ul li.pos_'+$item_position+' a').addClass('selected');
		});

		$('div.days_nav p.prev').live('click', function() {
			$(this).parent().parent().fadeOut('500');
			$(this).parent().parent().prev('div.section').fadeIn('1000');
			$item_position = $(this).parent().parent().prev('div.section').attr('class').substr(-1,1);
			$('#sections_tabs ul li a').removeClass('selected');
			$('#sections_tabs ul li.pos_'+$item_position+' a').addClass('selected');
		});
	}	
	/* // SECTION TABS */

//	$('#secondary ul.submeniu li a').live('click', function(){
//		var url = $(this).attr('href');
//		$('#secondary ul.submeniu li').removeClass('selected');
//		$(this).addClass('selected');
		//$('#container_content').empty(); 
		//alert(url);
		//return false;
		//myAjaxLoad2(url);
//		$('#container_content').load(url+' #content');
//		return false;
//	});

});



var myAjaxLoad2 = function(href) {		
	var selector = '#content';
	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=','');			  
	//return false;
	jQuery.ajax({
		url: href,
		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" ) { 
					alert(res.responseText);
			} else {
				// server err or 404
			}
		}
	});
}





function circuite(x){

	n = $('#sections_tabs ul li').length;
	current_circuit = '#sections_tabs ul li.pos_'+x+' a';
	if (x<=n)
	{
		$('#sections_tabs ul li a').removeClass('selected');
		$('#sections_tabs ul li a').removeClass('first');
		$(current_circuit).addClass('selected');
		x++;
		circuite_rotating = setTimeout( function() { circuite(x);}, 250);
		return false;
	} else {
		if (counter != 1)
		{
			circuite_rotating = setTimeout( function() { circuite(1);}, 250);
		} else {
			$('#sections_tabs ul li a').removeClass('selected');
			$('#sections_tabs ul li a').removeClass('first');
//			$('#sections_tabs ul li.pos_1 a').addClass('selected');
//			$('#page_sections div.pos_1').fadeIn('1000');
		}
		counter = 1;
		return false;
	}
}


function mouseOver() {
	if ($('#body_sections').hasClass('expanded'))
	{
	} else {
		if ($('#sections_tabs').hasClass('cat_selected'))
		{
		} else {
			$(this).addClass('selected');
			var index = $('#sections_tabs ul li').index(this) + 1;
			$('#page_sections div').fadeOut('500');
			$('#page_sections div.pos_'+index).fadeIn('1000');
		}
	}
}

function mouseOut() {
	if ($('#body_sections').hasClass('expanded'))
	{
	} else {
		if ($('#sections_tabs').hasClass('cat_selected'))
		{
		} else {
			$('#sections_tabs ul li').removeClass('selected');
			$('#page_sections div').fadeOut('500');
		}
	}
}





var myFormSubmit2 = function (dataString, form_action, reload) {
	var selector = '#page';
	$.ajax({
		type: "POST",  
		url: form_action,  
		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" ) {
					$('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(/<head(.|\s)*?\/head>/gi, ""))
							//.replace(/<script(.|\s)*?\/script>/gi, ""))

						// Locate the specified elements
						.find(selector) :

					// If not, just inject the full result
					res.responseText );
					if ($('#main_cnt').hasClass('my_account'))
					{
						setHash('page=indicii/');
					}
					$('p.loading').fadeOut(300);
					$('#main_cnt').fadeIn(300);
			} 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])
	{
		myAjaxLoad2(new_hash[1]);
	}
}

var removeHash = function(param) {
	var hash = '';
	document.location.hash = hash;
}
