


jQuery(document).ready(function () 
{
	
	

	
	if(jQuery("#change_this_banner").attr("id")=="change_this_banner")
		{
			setTimeout('change_banner_head()', 7000);
		}
	
	if(jQuery("#show_translation").val()==1)
		{
		  load_expressions(0);
		}
	
	jQuery(".list_expressions").click(function(){
				jQuery("#pageoffset").val("0");
				jQuery("#caut").val("");
				load_expressions(1);
				return false;
			});
	
	
	jQuery(".mt_admin_menu_mm").mouseenter(function(){
				var menu_id=jQuery(this).attr('id');
				hide_all_submenus();
				show_submenu(menu_id);
			});
	jQuery(".mt_admin_menu_mm").mouseleave(function(){
				var menu_id=jQuery(this).attr('id');
				hide_submenu(menu_id);
			});
	
	jQuery(".pf_filter_mm").mouseenter(function(){
				var menu_id=jQuery(this).attr('id');
				hide_all_subfilters();
				show_subfilter(menu_id);
			});
	jQuery(".pf_filter_mm").mouseleave(function(){
				var menu_id=jQuery(this).attr('id');
				hide_subfilter(menu_id);
			});
	
	if(jQuery("input[name='formname']").val()=="subcategorieseditor")
		{
			jQuery("select[name='type']").change(function(){
						load_ajax_get("#category_select", "actions/req_categories.php?type="+jQuery("select[name='type'] option:selected").val()+"&ucn="+jQuery("input[name='ucn']").val(), 1);
					});
		}
	
	if(jQuery("input[name='formname']").val()=="zoneeditor")
		{
			jQuery("select[name='tara']").change(function(){
						load_ajax_get("#oras_select", "actions/req_orase.php?tara="+jQuery("select[name='tara'] option:selected").val()+"&ucn="+jQuery("input[name='ucn']").val(), 1);
					});
		}
	
	if(jQuery("#map_canvas").attr("id")=="map_canvas")
		{
			initialize();
		}
		
	jQuery(".this_is_a_banner").click(function(){
						load_ajax_get("#dummyid", "actions/register_click.php?id="+jQuery(this).attr("title"), 0);
					});
	
	
	if(jQuery("input[name='b_change']").val()==1)
		{
			setTimeout('rotate_banner()', 10000);
		}
	
	initpage();
}
);


function rotate_banner()
{
	window.location="?loc="+jQuery("input[name='b_loc']").val()+"&offset="+jQuery("input[name='b_offset']").val()+"&type="+jQuery("input[name='b_type']").val();
}


var map;
var geocoder = null;

function initialize() {


  	var gzoom=5;
	var mplat=0;
	var mplng=0;
	
  if(jQuery("#gloc").val()!="")
  	{
		mappozx=jQuery("#gloc").val().split("#");
		if(mappozx.length>2)
			{
				mplat=parseFloat(mappozx[0]);
				mplng=parseFloat(mappozx[1]);
				gzoom=parseFloat(mappozx[2]);
				
				pune_punct(mplat,mplng,gzoom);
			}
		else
			{
				gzoom=parseFloat(mappozx[1]);
				geocoder = new google.maps.Geocoder();
				//alert(decodeURIComponent(mappoz[0]));

				geocoder.geocode( { 'address': decodeURIComponent(mappozx[0])}, function(results, status) {
				  if (status == google.maps.GeocoderStatus.OK) {
					mplat=results[0].geometry.location.lat();
					mplng=results[0].geometry.location.lng();
					pune_punct(mplat,mplng,gzoom);
				  }
				  else
				  {
					pune_punct(mplat,mplng,gzoom);
				  }
				  
				});
			}
	}
  else
  	{
		mplat=39;
		mplng=26;
		pune_punct(mplat,mplng,gzoom);
	}
	
	

  
}
var marker;
function placeMarker(location, refre) {
  var clickedLocation = new google.maps.LatLng(location);

  if(marker)
 	{ 
		marker.setPosition(location);
		marker.setVisible(true);
	}
  else
	{
      	marker = new google.maps.Marker({
  	    position: location,
        map: map
    });
	}
	
	if(refre)
		{
			jQuery("#gloc").val(location.lat().toString()+"#"+location.lng().toString()+"#"+map.getZoom().toString());
		}
	else
		{
			
			if(infowindow)
 				{
					infowindow.setPosition(location);
				}
			else
				{
				  var infowindow = new google.maps.InfoWindow(
				  { content: "<b>"+jQuery("input[name='map_company']").val()+"</b><br />"+jQuery("input[name='map_address']").val()
				  });
				}
			infowindow.open(map,marker);
			
		}

  map.setCenter(location);
}


function pune_punct(mplat,mplng,gzoom)
{
  var myLatlng = new google.maps.LatLng(mplat,mplng);
  //alert(mappoz[0]+" "+mappoz[1]+" "+gzoom);
  var myOptions = {
    zoom: gzoom,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,

      mapTypeControl: true,
      mapTypeControlOptions: {
          style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
          position: google.maps.ControlPosition.BOTTOM_LEFT
      }

  }
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  
  if(jQuery("#glocshow").val()==0)
  	{
		  google.maps.event.addListener(map, 'click', function(event) {
			placeMarker(event.latLng, true);
		  });
	}
  
  
  if(jQuery("#gloc").val()!="")
	{		

		var location=new google.maps.LatLng(mplat, mplng);
		placeMarker(location, false);
	}
}


function check_add_new_zona()
{
	if(jQuery("select[name='p_zona'] option:selected").val()=="ADD")
		{
			load_ajax_get("#mes_zona", "actions/req_add_new_zona.php", 1);
		}
}
function check_add_new_tara()
{
	if(jQuery("select[name='p_tara'] option:selected").val()=="ADD")
		{
			load_ajax_get("#mes_tara", "actions/req_add_new_tara.php", 1);
			load_ajax_get("#mes_oras", "actions/req_add_new_oras.php", 1);
			load_ajax_get("#mes_zona", "actions/req_add_new_zona.php", 1);
		}
	else
		{
			get_profil_oras();
		}
}
function check_add_new_oras()
{
	if(jQuery("select[name='p_oras'] option:selected").val()=="ADD")
		{
			load_ajax_get("#mes_oras", "actions/req_add_new_oras.php", 1);
			load_ajax_get("#mes_zona", "actions/req_add_new_zona.php", 1);
		}
	else
		{
			get_profil_zona();
		}
}

function change_banner_head()
{
	setTimeout('change_banner_head()', 7000);
	load_ajax_get_fade("#change_this_banner", "actions/req_banner_head.php", 1);
}

function hide_all_submenus()
{
	jQuery(".mt_menu_mm").removeClass("mt_admin_menu_mm_hov");
	jQuery(".mt_menu_sm").removeClass("mt_menu_sm_show");
}
function show_submenu(menu_id)
{
	var submenu_id="";
	submenu_id="#sm_"+menu_id.replace("mm_", "");
	jQuery("#"+menu_id).addClass("mt_admin_menu_mm_hov");
	jQuery(submenu_id).addClass("mt_menu_sm_show");
}
function hide_submenu(menu_id)
{
	var submenu_id="";
	submenu_id="#sm_"+menu_id.replace("mm_", "");
	jQuery("#"+menu_id).removeClass("mt_admin_menu_mm_hov");
	jQuery(submenu_id).removeClass("mt_menu_sm_show");
}


function hide_all_subfilters()
{
	jQuery(".pf_filter_mm").removeClass("pf_filter_mm_hov");
	jQuery(".pf_filter_sm").removeClass("pf_filter_sm_show");
}
function show_subfilter(menu_id)
{
	var submenu_id="";
	submenu_id="#f_sm_"+menu_id.replace("f_mm_", "");
	jQuery("#"+menu_id).addClass("pf_filter_mm_hov");
	jQuery(submenu_id).addClass("pf_filter_sm_show");
}
function hide_subfilter(menu_id)
{
	var submenu_id="";
	submenu_id="#f_sm_"+menu_id.replace("f_mm_", "");
	jQuery("#"+menu_id).removeClass("pf_filter_mm_hov");
	jQuery(submenu_id).removeClass("pf_filter_sm_show");
}

function initpage()
{
	tb_init('a.thickbox, area.thickbox, input.thickbox');


	if(jQuery("#translation_refresh").val()==1)
		{
		  	load_expressions(0);
			jQuery("#translation_refresh").val(0)
			initpage();
		}
	if(jQuery("#thikboxclose").val()==1)
		{
			tb_remove();
		}
	
	jQuery(".mt_trans_expr").mouseenter(function() {
				jQuery(this).addClass("mt_trans_expr_hover");
			});
	jQuery(".mt_trans_expr").mouseleave(function() {
				jQuery(this).removeClass("mt_trans_expr_hover");
			});
	jQuery(".pf_li_wl").mouseenter(function() {
				jQuery(this).addClass("pf_white");
			});
	jQuery(".pf_li_wl").mouseleave(function() {
				jQuery(this).removeClass("pf_white");
			});
	jQuery(".pf_li_wl_sub").mouseenter(function() {
				jQuery(this).addClass("pf_white_sub");
			});
	jQuery(".pf_li_wl_sub").mouseleave(function() {
				jQuery(this).removeClass("pf_white_sub");
			});
	
	
	
	jQuery('.mt_trans_human').focus(function() {
  				jQuery(this).select();
				jQuery(this).keyup( function() {
						jQuery(this).val(jQuery(this).attr("id"));
					});
			});
	jQuery('.mt_trans_human').click(function() {
  				jQuery(this).select();
				jQuery(this).keyup( function() {
						jQuery(this).val(jQuery(this).attr("id"));
					});
			});
	jQuery('.move_ajax').click(function(){
				file_url=jQuery(this).attr("href");
				jQuery.ajax({
				   type: "GET",
				   url: file_url,
				   cache: false,
				   success: function(html){
					 if(html=="OK")
					 	{
		  					load_expressions(0);
							initpage();
						}
					 else
					 	{
							alert("Operation failed! Try again!");
						}
					}
			
				 });
				return false;
			});
	
	jQuery(".delete_ajax").click(function(){
			if(window.confirm('Are you sure?'))
				{
				file_url=jQuery(this).attr("href");
				jQuery.ajax({
				   type: "GET",
				   url: file_url,
				   cache: false,
				   success: function(html){
					 if(html=="OK")
					 	{
		  					load_expressions(0);
							initpage();
						}
					 else
					 	{
							alert("Operation failed! Try again!");
						}
					}
			
				 });
				}
				return false;
			});
	
	jQuery(".ajlink_subscribe").click(function(){
				file_url=jQuery(this).attr("href")+"&subscribed="+jQuery("input[name='p_subscribed']").val();
				load_ajax_get("#categories_container", file_url, 1);
				return false;
		})

	
	jQuery('.page_ajax').click(function(){
				jQuery("#pageoffset").val(jQuery(this).attr("href"));
				load_expressions(0);
				return false;
			});
	
	
}


function get_categories()
{
	load_ajax_get("#mes_categories", "actions/req_categories_lead.php?type="+jQuery("#lead_type option:selected").val(), 1);
	load_ajax_get("#mes_subcategories", "actions/req_subcategories_lead.php?category=0", 1);
}
function get_subcategories()
{
	load_ajax_get("#mes_subcategories", "actions/req_subcategories_lead.php?category="+jQuery("#lead_category option:selected").val(), 1);
}


function get_categories_admin()
{
	load_ajax_get("#admin_category", "actions/req_categories_admin.php?type="+jQuery("#lead_type option:selected").val(), 1);
	load_ajax_get("#admin_subcategory", "actions/req_subcategories_admin.php?category=0", 1);
}
function get_subcategories_admin()
{
	load_ajax_get("#admin_subcategory", "actions/req_subcategories_admin.php?category="+jQuery("#lead_category option:selected").val(), 1);
}


function change_tara_profile()
{	
	load_ajax_get("#oras_subselect", "actions/req_orase_profile.php?tara="+jQuery("#profile_tara_select option:selected").val(), 1);
	load_ajax_get("#tara_choose", "actions/req_tara_choose.php?tara="+jQuery("#profile_tara_select option:selected").val(), 1);
	jQuery("#oras_choose").html("");
	jQuery("#zona_choose").html("");
	jQuery("#zona_subselect").html("");

}

function change_oras_profile()
{
	
	jQuery("#zona_choose").html("");
	if(jQuery("#profile_oras_select option:selected").attr("title")=="CHSUB")
		{
			load_ajax_get("#oras_choose", "actions/req_oras_choose.php?oras="+jQuery("#profile_oras_select option:selected").val(), 1);
			load_ajax_get("#zona_subselect", "actions/req_zone_profile.php?oras="+jQuery("#profile_oras_select option:selected").val(), 1);
		}
	else
		{
			choose_tara(jQuery("#profile_oras_select option:selected").attr("id"), jQuery("#profile_oras_select option:selected").attr("title"));
		}
}
function change_zona_profile()
{	
	choose_tara(jQuery("#profile_zona_select option:selected").attr("id"), jQuery("#profile_zona_select option:selected").attr("title"));
}


function choose_tara(unique_name, name)
{
	jQuery("input[name='search_location_fixed']").val(unique_name);
	jQuery("input[name='search_city']").val(name);
	tb_remove();
}


function change_type_profile()
{
	load_ajax_get("#category_subselect", "actions/req_categories_profile.php?type="+jQuery("#profile_type_select option:selected").val(), 1);
	jQuery("#subcategory_subselect").html("");
	if(jQuery("#catchoose").val()==1)
		{
			load_ajax_get("#type_choose", "actions/req_type_choose.php?type="+jQuery("#profile_type_select option:selected").val(), 1);
			jQuery("#category_choose").html("");
			jQuery("#subcategory_choose").html("");
		}
	else
		{
			jQuery("#category_subscribe").html("");
			jQuery("#subcategory_subscribe").html("");
		}
}

function change_category_profile()
{
	load_ajax_get("#subcategory_subselect", "actions/req_subcategories_profile.php?category="+jQuery("#profile_category_select option:selected").val(), 1);
	if(jQuery("#catchoose").val()==1)
		{
			if(jQuery("#profile_category_select option:selected").attr("title")=="CHSUB")
				{
					load_ajax_get("#category_choose", "actions/req_category_choose.php?category="+jQuery("#profile_category_select option:selected").val(), 1);
				}
			else
				{
					choose_type(jQuery("#profile_category_select option:selected").attr("id"), jQuery("#profile_category_select option:selected").attr("title"));
				}
			jQuery("#subcategory_choose").html("");
		}
	else
		{
			if(jQuery("#profile_category_select option:selected").attr("title")=="CHSUB")
				{
					load_ajax_get("#category_subscribe", "actions/req_category_subscribe.php?category="+jQuery("#profile_category_select option:selected").val(), 1);
				}
			else
				{
					subcribe_category(jQuery("#profile_category_select option:selected").val());
				}
			jQuery("#subcategory_subscribe").html("");
		}
}
function change_subcategory_profile()
{
	if(jQuery("#catchoose").val()==1)
		{
			choose_type(jQuery("#profile_subcategory_select option:selected").attr("id"), jQuery("#profile_subcategory_select option:selected").attr("title"));
			//load_ajax_get("#subcategory_choose", "actions/req_subcategory_choose.php?subcategory="+jQuery("#profile_subcategory_select option:selected").val(), 1);
		}
	else
		{
			subcribe_subcategory(jQuery("#profile_subcategory_select option:selected").val());
			//load_ajax_get("#subcategory_subscribe", "actions/req_subcategory_subscribe.php?subcategory="+jQuery("#profile_subcategory_select option:selected").val(), 1);
		}
}




function choose_type(unique_name, name)
{
	jQuery("input[name='search_category_fixed']").val(unique_name);
	jQuery("input[name='search_category']").val(name);
	tb_remove();
}


function subcribe_category(catid)
{
	load_ajax_get("#categories_container", "actions/subcribe_category.php?category="+catid+"&subscribed="+jQuery("input[name='p_subscribed']").val(), 0);
	tb_remove();
	
}
function subcribe_subcategory(catid)
{
	load_ajax_get("#categories_container", "actions/subcribe_subcategory.php?subcategory="+catid+"&subscribed="+jQuery("input[name='p_subscribed']").val(), 0);
	tb_remove();
}

function load_ajax_get(el_id, file_url, keepwidth)
{
	jQuery.ajax({
	   type: "GET",
	   url: file_url,
	   cache: false,
	   beforeSend: function(html){
		  	if(keepwidth)
				{
					var crtheight=jQuery(el_id).height();
					var strheight=crtheight.toString();
					jQuery(el_id).html("<div style=\"height:"+strheight+"px;\"><img src=\"img_mt/loadingAnimation.gif\" /></div>");
				}
			else
				{
		 			jQuery(el_id).html("<img src=\"img_mt/loadingAnimation.gif\" />");
				}
		},
	   success: function(html){
		 jQuery(el_id).html(html);
		 initpage();
		}

	 });
}


function load_ajax_get_fade(el_id, file_url, keepwidth)
{
	jQuery.ajax({
	   type: "GET",
	   url: file_url,
	   cache: false,
	   beforeSend: function(html){
		  	if(keepwidth)
				{
					var crtheight=jQuery(el_id).height();
					var strheight=crtheight.toString();
				}
		},
	   success: function(html){
		 var currentDate = new Date();
		 var idel="bann_"+currentDate.getTime().toString();
		 jQuery(el_id).html(jQuery(el_id).html()+'<div class="pf_cont_banner" id="'+idel+'">'+html+'</div>');
		 jQuery("#"+idel).fadeIn(1000, function() { jQuery(el_id).html(html); });
		 initpage();
		}

	 });
}

function submit_ajax_form(el_id, file_url, formdata)
{
	jQuery.ajax({
	   type: "POST",
	   url: file_url,
	   cache: false,
	   data: formdata,
	   success: function(html){
		  if(el_id!="")
		  	{
				 jQuery(el_id).html(html);
			}
		 initpage();
		}

	 });
}


function load_expressions(sw)
{
	prefixv=jQuery("#prefix").val();
	po=jQuery("#pageoffset").val();
	if(sw==1)
		{
			load_ajax_get("#translation_container", "actions/translation-list.php?prefix="+prefixv+"&o="+po, 1);
		}
	else
		{
			cuvantcautat=jQuery("#caut").val();
			load_ajax_get("#translation_container", "actions/translation-list.php?prefix="+prefixv+"&o="+po+"&caut="+cuvantcautat, 1);
		}
}

function jq_ajax_form(formobj)
{
	formid="#"+formobj.id;
	if(formid=="translation_search")
		{
			jQuery("#pageoffset").val("0");
		}
	clearformid=formobj.id;
	formdata=jQuery(formid).serialize();
	formsubmiturl=jQuery(formid).attr("action");
	fillid=jQuery("#fill_"+clearformid).val();
	submit_ajax_form("#"+fillid, formsubmiturl, formdata);

	return false;
}