var opened_content = false;
var custom_content = false;
$(function(){
	addContent();
	$("#contact_page,#static_page,#sugestii_page").css({
		position:"absolute",
		opacity:0
	});

	$(".links a").click(function(){
		var idss = $(this).attr("idss")*1;
		log(idss)
		switch(idss){
			case 1:
				custom_content = true;
				makeSmallestAll(custom_content);
				$("#contact_page,#sugestii_page").stop(true,true).animate({opacity:0},"fast",function(){$(this).hide()});
				$("#static_page").stop(true,true).animate({opacity:1},"fast",function(){$(this).show()});
				$("#static_page .title").html(pages.id_83.subpages.id_84.name);
				$("#static_page .left.a1").html(pages.id_83.subpages.id_84.desc).scrollbar();
			break;
			case 2:
				custom_content = true;
				makeSmallestAll(custom_content);
				$("#contact_page,#sugestii_page").stop(true,true).animate({opacity:0},"fast",function(){$(this).hide()});
				$("#static_page").stop(true,true).animate({opacity:1},function(){$(this).show()});
				$("#static_page .title").html(pages.id_83.subpages.id_86.name);
				$("#static_page .left.a1").html(pages.id_83.subpages.id_86.desc).scrollbar();
			break;
			case 3:
				custom_content = true;
				makeSmallestAll(custom_content);
				$("#contact_page,#sugestii_page").stop(true,true).animate({opacity:0},"fast",function(){$(this).hide()});
				$("#static_page").stop(true,true).animate({opacity:1},"fast",function(){$(this).show()});
				$("#static_page .title").html(pages.id_83.subpages.id_85.name);
				$("#static_page .left.a1").html(pages.id_83.subpages.id_85.desc).scrollbar();
			break;
			case 4:
			$('.mi1').trigger('click');
			$('.mi1').find('.smi').eq(0).trigger('click');
			break;
			case 5:
			$('.mi2').trigger('click');
			$('.mi2').find('.smi').eq(0).trigger('click');
			break;
			case 6:
			$('.mi3').trigger('click');
			$('.mi3').find('.smi').eq(0).trigger('click');
			break;
			case 7:
			$('.mi4').trigger('click');
			$('.mi4').find('.smi').eq(0).trigger('click');
			break;
			case 8:
			$('.mi5').trigger('click');
			$('.mi5').find('.smi').eq(0).trigger('click');
			break;
			case 9:
			$('.mi6').trigger('click');
			$('.mi6').find('.smi').eq(0).trigger('click');
			break;
			case 10:
				custom_content = true;
				makeSmallestAll(custom_content);
					$("#static_page,#sugestii_page").stop(true,true).animate({opacity:0},"fast",function(){$(this).hide()});
					$("#contact_page").stop(true,true).animate({opacity:1},function(){$(this).show()});
				
			break;
			default:
				window.location = "/";
			break;
		}
		return false;
	});
	$("#hmenu a").click(function(){
var ids = $(this).attr("ids");
if(ids==0){
window.location = "/";
return false;
}
		$("#hmenu a").removeClass("selected");
		$(this).addClass("selected");
		custom_content = true;
		makeSmallestAll(custom_content);
		
		if(ids=="contact_sugestii"){
			$("#contact_page,#static_page").stop(true,true).animate({opacity:0},"fast",function(){$(this).hide()});
			$("#sugestii_page").stop(true,true).animate({opacity:1},function(){$(this).show()});
		}else if(ids=="contact_contact"){
			$("#static_page,#sugestii_page").stop(true,true).animate({opacity:0},"fast",function(){$(this).hide()});
			$("#contact_page").stop(true,true).animate({opacity:1},function(){$(this).show()});
		}else{

			$("#contact_page,#sugestii_page").stop(true,true).animate({opacity:0},"fast",function(){$(this).hide()});
			
			if(ids==85){
$("#static_page").stop(true,true).animate({opacity:1},function(){$(this).show()});
				$("#static_page .title").html(pages.id_83.subpages.id_84.name);
				$("#static_page .left.a1").html(pages.id_83.subpages.id_84.desc);
			}else if(ids==86){
$("#static_page").stop(true,true).animate({opacity:1},function(){$(this).show()});
				$("#static_page .title").html(pages.id_83.subpages.id_85.name);
				$("#static_page .left.a1").html(pages.id_83.subpages.id_85.desc);
			}else{
				window.location = "/";
			}
		}
		return false;
	})
	
	/*$('.video a[rel=video]').each(function(i,el){
		$(el).flash({
			swf: 'media/toob_embeddable.swf',
			flashvars: {
				url: $(el).attr("href"),
				previewImageUrl: '',
				autoPlay: false 
			},
			height: 385, 
			width: 640,
			hasVersion: 10,
			wmode: "transparent",
			quality: "high",
			menu: false
		});
	});*/
	
	$(".left_arr").hide().click(function(){
		var pos = parseInt($(".image_gal_holder:first-child").css("left"));
		log("left "+pos)
		if(pos<0){
			$(".image_gal_holder").each(function(i,el){
				$(el).animate({
					left:parseInt($(el).css("left"))+169*5
				},"fast");
			});
		}
	});
	$(".right_arr").hide().click(function(){
		var pos = parseInt($(".image_gal_holder:last-child").css("left"));
		log("right "+pos);
		log(pos>676);
		if(pos>676){
			$(".image_gal_holder").each(function(i,el){
				$(el).animate({
					left:parseInt($(el).css("left"))-169*5
				},"fast");
			});
		}
	});
	
	$(".over_img").css({display:"block",opacity:0});
	
	$(".content_holder").css({width:0});
	
	$(".content_holder .holder .image_bg .images_scroller .image_gal_holder").each(function(i,el){
		$(el).css({left:i*169})
	});
	
	$(".smi").click(function(e){
		if(e.stopPropagation)e.stopPropagation();
		
		$(".not_selected_menu_for_animation").animate({width:0},"slow",function(){
			$(".not_selected_menu_for_animation").hide()
		});
		if($(".not_selected_menu_for_animation").hasClass("mi6")){
			$(".not_selected_menu_for_animation").prev().hide();
		}else{
			$(".not_selected_menu_for_animation").next().hide();
		}
		$(".not_selected_menu_for_animation").addClass("the_menu_that_is_closed");
		$(".content_holder").css({display:"block"}).animate({width:870},"slow");
		//$(".link").animate({opacity:0,width:37},"slow");
		
		/*revert*/
			$(".mi.asd .over_img").animate({opacity:0},"fast");
			$(".mi.asd .link").css({textAlign:"center"}).animate({
				width:37,
				marginLeft:5,
				marginRight:5,
				marginTop:380,
				paddingTop:40,
				height:55,
				opacity:0
			},"slow",function(){opened_content = true;}).css({borderColor:"#ffffff"}).children(".width_holder").animate({
				width:126,
				marginTop:0,
				marginLeft:0,
				marginBottom:0
			},"slow");
			$(".mi.asd.not_selected_menu_for_animation .link").children(".submenu").slideUp("slow");
			$(".mi.asd").removeClass("not_selected_menu_for_animation").addClass("selected_menu_for_animation");
			/**/
			
		loadPage(this);
	});
	
	$(".mi.asd").hover(function(){
		if(opened_content){
			var o = $(this).attr("class");
			$(".mi.asd.selected_menu_for_animation .link").each(function(i,el){
				if($(el).attr("class")!=o) $(el)
					.stop().animate({width:8,opacity:0
					//	,marginLeft:(custom_content?2:14),marginRight:(custom_content?2:14)
						},"fast");
			});
			$(this).children(".link").stop().animate({width:126,opacity:1},"fast");
		}
	},function(){
		if(opened_content){
			$(".mi.asd.selected_menu_for_animation .link").each(function(i,el){
				$(el)
					.stop().animate({width:(custom_content?28:37),opacity:0
						,marginLeft:5,marginRight:5
					},"fast");
			});
		}
	});
	
	$(".mi.asd").click(function(){
		if(opened_content){
			if(custom_content){
				custom_content = false;
				makeSmallestAll(custom_content);
			}
			loadFirstPage(this);
		}else if(!$(this).hasClass("not_selected_menu_for_animation")){
			$(".mi.asd").removeClass("not_selected_menu_for_animation").addClass("selected_menu_for_animation");
			$(this).removeClass("selected_menu_for_animation").addClass("not_selected_menu_for_animation");
			$(".mi.asd.selected_menu_for_animation .over_img").animate({opacity:0},"fast");
			$(".mi.asd.selected_menu_for_animation .link").css({textAlign:"center"}).animate({
				width:126,
				marginLeft:5,
				marginRight:5,
				marginTop:380,
				paddingTop:40,
				height:55
			},"slow").css({borderColor:"#ffffff"}).children(".width_holder").animate({
				width:126,
				marginTop:0,
				marginLeft:0,
				marginBottom:0
			},"slow");
			$(".mi.asd.selected_menu_for_animation .link").children(".submenu").slideUp("slow");
			$(this).children(".link").css({textAlign:"left"}).animate({
				width:395,
				marginRight:14,
				marginLeft:14,
				marginTop:150,
				paddingTop:0,
				height:194
			},"slow").css({borderColor:"#000000"}).children(".width_holder").animate({
				width:5,
				marginTop:10,
				marginLeft:10,
				marginBottom:10
			},"slow");
//			$(this).children(".over_img").animate({opacity:1},"slow");
			$(this).children(".over_img").animate({opacity:1},500);
			$(this).children(".link").children(".submenu").slideDown("slow");
		}
	});
	if(listNews.length>0){
		loadNews();
		$('#news_rodotexsocial').show();
	}else{
		$('#news_rodotexsocial').hide();
	}
});

function largeNews(id){
	if(!id)
		id=0;
	$('#contact_page,#sugestii_page').hide();
	$('#static_page').find('.title').html(listNews[id].nume+"");
	var menu = '<div style="float:right;width:300px;height:447px;background:#861452;margin-right:-12px;margin-top:-20px;overflow:hidden;" id="scroll_all_news"><div style="font-size:18px;margin:10px;">Toate Stirile</div><br/>';
	for(var i=0;i<listNews.length;i++){
		menu += '<a href="#" onClick="largeNews('+i+'); return false;" style="text-decoration:none;color:#fff;margin-left:20px;display:block;padding:5px 0px;">'+listNews[i].nume+'</a>';
	}
	menu +='</div>';
	var content = '<div style="float:left;width:510px;height:400px;overflow:hidden;" id="scroll_news">'+listNews[id].desc+'</div>';
	
	$('#static_page').find('.content .left.a1').html($(content));
	$('#static_page').find('.content .left.a1').append($(menu));
	
	$("#static_page").css({
		position:"absolute",
		opacity:1
	}).show();
	if(!custom_content){
		custom_content = true;
		makeSmallestAll(custom_content);
	}
	$("#static_page .left").css({margin:0,padding:20});
	$('#scroll_news').scrollbar();
	$('#scroll_all_news').scrollbar({pas:100});
}

function loadNews(){
	var newsDOM = '';
	for(var i=0;i<listNews.length;i++){
		newsDOM = '<li class="news_l" style="padding:10px;position:relative;margin-left:0px;width:400px;margin-left:10px;" onClick="largeNews('+i+');"> \
					<div style="color:#ccc;float:left;font-weight:bold;" id="news_title">'+listNews[i].nume+'&nbsp;&nbsp;&nbsp;</div><div style="float:left;font-size:10px;color:#861452;margin-top:2px;">'+listNews[i].data+'</div> \
					<div style="color:#fff;clear:both;margin-top:20px;" id="news_content">'+listNews[i].desc.substr(0,255)+'</div> \
				</li>';	
		$('#news .list_news ul').append($(newsDOM));
	}
	$(".list_news").jCarouselLite({
		btnNext: ".next",
		btnPrev: ".prev",
		circular: true,
		visible: 1,
		speed: 600,
		scroll:1,
		nik:false,
		auto:true,
		timers:5000
	});
}

function makeSmallestAll(a){
	$("#static_page .left").css({margin:20,padding:0});
	if(a){
		opened_content = true;
		$(".the_menu_that_is_closed").removeClass('the_menu_that_is_closed').css({"width":"",opacity:1}).show();
		$(".content_holder").hide("slow");
		$(".mi.asd").removeClass("not_selected_menu_for_animation").addClass("selected_menu_for_animation");
		$("#container_static").show("slow");
		$(".link").each(function(i,el){
			$(el).animate({width:28,opacity:0,marginLeft:5,marginRight:5},"slow");
		});
		$(".hspace").show()
	}else{
		$("#container_static").hide("slow");
		$(".content_holder").css({width:0,display:"block"}).animate({width:870},"slow");
	}
}

function loadFirstPage(o){
	if(o){
		opened_content = false;
		/*
		 * open last menu
		 */
		var h = $(".the_menu_that_is_closed .link")
		$(".the_menu_that_is_closed").css({width:"auto",opacity:1});
		if($(".the_menu_that_is_closed").hasClass("mi6")){
			$(".the_menu_that_is_closed").prev().show();
		}else{
			$(".the_menu_that_is_closed").next().show();
		}
		$(".the_menu_that_is_closed").show().removeClass("the_menu_that_is_closed");
		/*
		 * close selected menu
		 */
		//loadPage(this);
		//$(o).animate({width:0},"slow",function(){$(o).hide()});
		if($(o).hasClass("mi6")){
			$(o).prev().hide();
		}else{
			$(o).next().hide();
		}
		$(o).addClass("the_menu_that_is_closed");
		
		$(".link").each(function(i,el){
			$(el).animate({width:37,opacity:0},"slow");
				
				if($(el).parent().hasClass("the_menu_that_is_closed")){
					$(el).parent().animate({width:0,opacity:0},"slow",function(){
						$(this).hide();
						opened_content = true;
					});
				}
		});
		/*
		 * change content
		 */
		var data = $($($(o).children(".link")[0]).children(".submenu").children(".left")[0]).children(".smi:first-child");
		loadPage(data);
	}
}

function addContent(){
	for(var i in pages){
		$("#"+i).empty();
		var aux = 0;
		for(var j in pages[i].subpages){
			if(aux % 6==0){
				$("#"+i).append($("<div class='left'>"));
			}
			aux++;
			$("#"+i+" .left:last-child").append('<div class="smi" pds="'+pages[i].id+'" ids="'+pages[i].subpages[j].id+'">'+pages[i].subpages[j].name+'</div>');
		}
		$("#"+i).append("<div class='breaker'></div>");
	}
}

function log(s){
	try{
		console.log(s);
	}catch(e){}
}
function loadPage(o){
	var obj = pages["id_"+$(o).attr("pds")];
	if($(".header").html()!=stripTags(obj.name)){
		$(".header").animate({color:"#E3007B"},"fast",function(){
			$(this).html(stripTags(obj.name)).animate({color:"#ffffff"},"fast");
		});
		var f = false;
		$(".c_submenu .c_sm").animate({opacity:0},"fast",function(){
			if(!f){
				f= true;
				$(".c_submenu").html("");
				for(var i in obj.subpages){
					$(".c_submenu").append("<div class='c_sm' id='s"+i+"' pds='"+obj.id+"' ids='"+obj.subpages[i].id+"'>"+stripTags(obj.subpages[i].name)+"</div>");
				}
				$(".c_sm").click(function(){
					loadPage(this);
				});
				$(".c_submenu .c_sm#sid_"+$(o).attr("ids")).addClass("selected");
			}
		});
	}
	$(".c_submenu .c_sm.selected").removeClass("selected");
	$(".c_submenu .c_sm#sid_"+$(o).attr("ids")).addClass("selected");
	$(".text").animate({color:"black"},"fast",function(){
		$(this).animate({color:"white"},"fast").html("<h3>"+(pages["id_"+$(o).attr("pds")].subpages["id_"+$(o).attr("ids")].name)+"</h3>"+(pages["id_"+$(o).attr("pds")].subpages["id_"+$(o).attr("ids")].desc));
		$('.scrollbar_c').scrollbar();
	});
	$(".images_scroller").animate({opacity:0},"fast",function(){
		$(this).html("").animate({opacity:1},"fast");
		for(var i in obj.subpages["id_"+$(o).attr("ids")].images){
			$(this).append(
				'<div class="image_gal_holder" style="left: '+(i*169)+'px;">'+
					'<a rel="grup" href="'+obj.subpages["id_"+$(o).attr("ids")].images[i]+'"><div class="hover" style="display: block;"></div>'+
					'<img width="162" height="91" class="totemg" src="thumbs/'+obj.subpages["id_"+$(o).attr("ids")].images[i]+'" alt=""></a>'+
				'</div>'
			);
		}
		if(i<5){
			$(".left_arr").hide();
			$(".right_arr").hide();
		}else{
			$(".left_arr").show();
			$(".right_arr").show();
		}
		$('.images_scroller a[rel=grup]').fancybox({overlayColor:'#000'});
	});
	
	$(".video_holder").animate({opacity:0},"fast",function(){
		$(this).empty().animate({opacity:1},"fast");
		for(var i in obj.subpages["id_"+$(o).attr("ids")].videos){
			$(this).append(
				'<div class="video_i">'+
					'<a rel="video" href="#playVideo-'+i+'">'+
						'<div class="hover" style="display: block; opacity: 0;"></div>'+
						'<img height="60px" width="106px" src="thumbs/'+obj.subpages["id_"+$(o).attr("ids")].videos[i]+'" alt="">'+
					'</a>'+
					'<div id="playVideoHolder-'+i+'" style="display:block;">'+ 
						'<div id="playVideo-'+i+'"></div>'+ 
					'</div>'+ 
				'</div>'
			);
			$('#playVideo-'+i).flash({
																swf: 'toob_embeddable.swf',
																flashvars: {
																	url: getVideoUrl(obj.subpages["id_"+$(o).attr("ids")].videos[i]),
																	previewImageUrl: obj.subpages["id_"+$(o).attr("ids")].videos[i],
																	autoPlay: false }, 
																height: 385, 
																width: 640, 
																hasVersion: 10,
																wmode: "transparent", 
																quality: "high",
																menu: false 
															});
		}
		$('.video a[rel=video]').fancybox({overlayOpacity:0.7,overlayColor:'#000'});
		
		
		$('.images_scroller a[rel=grup]').fancybox({overlayOpacity:0.7,overlayColor:'#000'});
	});
	
}

function stripTags(s) {
    return s.replace(/<\/?[^>]+>/gi, ' ');
}

function getVideoUrl(url){
	return url.replace("media/","")+".flv";
}

function validate(email){
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	if(reg.test(email) == false) {
		return false;
	}else{
		return true;
	}
}

function sendReclamatie(){
	var from  = $('#sugestie_email').val();
	var nume  = $('#sugestie_nume').val();
	var mesaj = $('#sugestie_mesaj').val();
	var copie = $('#sugestie_copie:checked').val();
	
	if(!validate(from)){
		$("#sugestie_loading").html("Adresa de email este invalida!");
		return;
	}
	$("#sugestie_loading").html("se trimite mesajul ...");

	var request = $.ajax({
	  url: "main/sendReclamatie",
	  type: "POST",
	  data: {
			 from:from,
			 nume:nume,
			 mesaj:mesaj,
			 copie:copie
			},
	  dataType: "json"
	});

	request.done(function(msg) {
		if(msg.err){
			$("#sugestie_loading").html(msg.mesaj);
		}else{
			$('#sugestie_nume').val("");
			$('#sugestie_email').val("");
			$('#sugestie_mesaj').val("");
			$("#sugestie_loading").html(msg.mesaj);
		}
		
	});

	request.fail(function(jqXHR, textStatus) {
	  $("#sugestie_loading").html("Eroare la trimitere!");
	});
}

function sendContact(){
	var from    = $('#contact_email').val();
	var nume    = $('#contact_nume').val();
	var mesaj   = $('#contact_mesaj').val();
	var subiect = $('#contact_subiect').val();
	var copie   = $('#contact_copie:checked').val();
	
	if(!validate(from)){
		$("#contact_loading").html("Adresa de email este invalida!");
		return;
	}
	$("#contact_loading").html("se trimite mesajul ...");

	var request = $.ajax({
	  url: "main/sendContact",
	  type: "POST",
	  data: {
			 from:from,
			 nume:nume,
			 mesaj:mesaj,
			 subiect:subiect,
			 copie:copie
			},
	  dataType: "json"
	});

	request.done(function(msg) {
		if(msg.err){
			$("#contact_loading").html(msg.mesaj);
		}else{
			$('#contact_nume').val("");
			$('#contact_email').val("");
			$('#contact_mesaj').val("");
			$("#contact_loading").html(msg.mesaj);
		}
		
	});

	request.fail(function(jqXHR, textStatus) {
	  $("#contact_loading").html("Eroare la trimitere!");
	});
}

