$().ready(function(){
	$("#headerTop .menu LI:first-child").addClass("first");
	$("#headerTop .menu LI:last-child").addClass("last");
	$("#headerTop .menu LI[class!=ov]").hover(
		function() {
			$(this).addClass("ov");
		},
		function() {
			$(this).removeClass("ov");
		}
	);
	$("#headerTop .menu LI.first[class!=firstOv first]").hover(
		function() {
			$(this).addClass("firstOv");
		},
		function() {
			$(this).removeClass("firstOv");
		}
	);
	$("#headerTop .menu LI.last[class!=lastOv last]").hover(
		function() {
			$(this).addClass("lastOv");
		},
		function() {
			$(this).removeClass("lastOv");
		}
	);
	$(".admin .boxWhite2 UL LI[class!=ov]").hover(
		function() {
			$(this).addClass("ov");
		},
		function() {
			$(this).removeClass("ov");
		}
	);
	$().ready(function(){
		$("#user_branch_select").change(function(){
			$.ajax({
				url: baseURL+"users/set_branch",
				async: false,
				data: {"branch_id":$(this).val()},
				type: "post",
				success: function(data){
					window.location.reload();
				}
			});
		});
	});
	/******************* add classes, odd, alt, clear margins */
	$("#sidebar > .widget:last").addClass("widgetLast");
	$("#content > .box:last, .boxTopLeft > .box:last, .boxTopRight > .box:last").addClass("boxLast");
	$(".listPromo LI:odd, .listType1 LI:odd").addClass("alt");
	$(".widgetRecommendations").children("UL").children("LI:odd").addClass("alt");

	/********************* add target blank in external links */
	$(function(){
		$("a[rel*='external']").click(function(){
			this.target = "_blank";
		});
	});

	/***************************** checkbox and radio replace */
	$('input').checkBox();
	/*$('#toggle-all').click(function(){
		$('#example input[type=checkbox]').checkBox('toggle');
		return false;
	});
	$('#check-all').click(function(){
		$('#example input[type=checkbox]').checkBox('changeCheckStatus', true);
		return false;
	});
	$('#uncheck-all').click(function(){
		$('#example input[type=checkbox]').checkBox('changeCheckStatus', false);
		return false;
	});
	$('#check-2').click(function(){
		$('#example input[type=radio]:eq(1)').checkBox('changeCheckStatus', true);
		return false;
	});
	$('#native').click(function(){
		//native methods
		$('#example input[type=radio]:eq(0)').attr({checked: true, disabled: true})
			//reflect the current state
			.checkBox('reflectUI');
		return false;
	});*/

	/* custom select */
	$('select#category_id, select#level, select#time').selectmenu({
		width: 396,
		maxHeight:250
	});
	$('DIV.double SELECT').selectmenu({
		width: 137,
		maxHeight:250
	});
	$('select#user_branch_select').selectmenu({
		width: 190,
		maxHeight:250
	});
	$('SELECT').selectmenu({
		width: 142,
		maxHeight:250
	});


	/* tabs */
//	$("#tabsCountry").tabs();
	$("#tabsGeneral").tabs();
	$(".flashMap").media({
		width: 300,
		height: 300
	});

	/************************
	 * filtrele de cautare
	 * *********************/
	var continent_change = function(){
		var el = this;
		$.ajax({
			url : baseURL+"home/ajax_related_options",
			type: "POST",
			data: {"option_id":$(this).val(),"compare":0,"limit":-1},
			success: function(data){
				var result = eval("("+data+")");
				$("#search_country").remove()
				$("ul[id^=search_country_menu]").remove();
				$("a[id^=search_country_button]").remove();
				$("#search_country_holder").append('<select name="country" id="search_country"></select>');
				$("#search_country").append('<option value="">Tara</option>');
				$.each(result.data,function(k,v){
					$("#search_country").append('<option value="'+v.id+'">'+v.name+'</option>');
				});
				$("#search_country").selectmenu({
					width: 142,
					maxHeight:250
				});
				$("#search_country").change(country_change);
				reset_destination();
			}
		});
	};
	$("#search_form").submit(function(e){
		var search_location = $(this).find("input:checked").val();
		if(search_location == "web"){
			var keyword = $(this).find("input[name=keyword]").val();
			if(keyword != ""){
				window.location = "http://www.okidoki.ro/search?query="+keyword;
			}
			e.preventDefault();
			return false;
		}
	});
	var country_change = function(){
		return false;
		var el = this;
		$.ajax({
			url : baseURL+"home/ajax_related_options",
			type: "POST",
			data: {"option_id":$(this).val(),"compare":0},
			success: function(data){
				var result = eval("("+data+")");
				$("#search_destination").remove()
				$("ul[id^=search_destination_menu]").remove();
				$("a[id^=search_destination_button]").remove();
				$("#search_destination_holder").append('<select name="destination" id="search_destination"></select>');
				$("#search_destination").append('<option value="">Destinatie</option>');
				$.each(result.data,function(k,v){
					$("#search_destination").append('<option value="'+v.id+'">'+v.name+'</option>');
				});
				$("#search_destination").selectmenu({
					width: 142,
					maxHeight:250
				});
			}
		});
	};
	var category_change = function(e){
		$("#search_price").remove()
		$("ul[id^=search_price]").remove();
		$("a[id^=search_price]").remove();
		$("#search_price_holder").append('<select name="price" id="search_price"></select>');
		$("#search_price").append('<option value="">Pret EUR</option><option value="0-100">0 - 100</option><option value="101-300">101 - 300</option><option value="301-500">301 - 500</option><option value="501-1000">501 - 1000</option><option value="1001-0">&gt 1001</option>');
		$("#search_price").selectmenu({
			width: 142,
			maxHeight:250
		});
	}
	$("#search_category_id").change(category_change);
	var reset_destination = function(){
		return false;
		$("#search_destination").remove()
		$("ul[id^=search_destination_menu]").remove();
		$("a[id^=search_destination_button]").remove();
		$("#search_destination_holder").append('<select name="destination" id="search_destination"></select>');
		$("#search_destination").append('<option value="">Destinatie</option>');
		$("#search_destination").selectmenu({
			width: 142,
			maxHeight:250
		});

	}
	$("#search_continent").change(continent_change);
	$("#search_country").change(country_change);
	$("#btn_cancel").click(function(){
		$.ajax({
			type: "get",
			url: baseURL+"ads/cancel_edit",
			success: function(){
				window.location = baseURL+"dashboard";
			}
		});
	});
	$("form.comment").submit(function(e){
		var data = $(this).serialize();
		var error_text = {
			"1": "Articolul nu exista",
			"2": "Datele trimise nu sunt corecte"
		};
		var errors = comments_utils.validate_comments_form(data);
		if(errors.length > 0){
			comments_utils.show_comments_errors(errors.join("\n"));
			return false;
		}
		$.ajax({
			url: baseURL+"comments_manager/ajax_create",
			data: data,
			type: "POST",
			success: function(data){
				var result = eval("("+data+")");
				if(result.error > 0){
					comments_utils.show_comments_errors(error_text[result.error]);
					return false;
				} else {
					comments_utils.refresh_comments({"page":1});
					$("form.comment").find("input[type=text], textarea").val("");
				}
				comments_utils.refresh_captcha($("#comments_captcha"));
			}
		});
		e.preventDefault();
		return false;
	});
	$("div.boxComments div.pagesNav a").click(comments_utils.pagination_events);
	$("#comments_captcha").click(function(){
		comments_utils.refresh_captcha($(this));
	});
});
function alert_continent(continent) {
	$.ajax({
	      url: "/maps/continent",
	      global: false,
	      type: "POST",
	      data: ({continent : continent}),
	      dataType: "html",
	      success: function(msg){
	         $('#maps_target').empty().html(msg);
	      }
	   }
	)
}
function alert_tara(country) {
	split_index = country.indexOf('_');
	
	if(split_index >= 0)
		continent = country.substr(0,split_index)
	
	$.ajax({type:"GET",url:"/maps/xml/"+continent+".xml",dataType:"xml",success:function(xml){
		$(xml).find('tara').each(function(){
			url = $(this).attr('url')
			 if( url == country+'.swf' ){
				 $.ajax({
				      url: "/maps/country",
				      global: false,
				      type: "POST",
				      data: ({country : $(this).attr('name') }),
				      dataType: "html",
				      success: function(msg){
				         $('#maps_target').empty().html(msg);
				      }
				})
			 }
		});
	}});
}
var comments_utils = {
	current_page: 1,
	show_comments_errors : function(str){
		alert(str);
	},
	pagination_events: function(e){
		e.preventDefault();
		e.stopPropagation();
		var page = $(this).attr("href").match(/\d+$/);
		if(page == null){
			page = 1;
		} else {
			page = page[0];
		}
		comments_utils.refresh_comments({"page":page});
		return false;
	},
	validate_comments_form : function(data){
		var errors = [];
		if($("#comment-name").val() == ""){
			errors.push("Numele este obligatoriu");
		}
		if($("#comment-email").val() == ""){
			errors.push("Adresa de email este obligatorie");
		} else {
			var val = $("#comment-email").val();
			var reg = /([a-z0-9!#$%&'*+\:/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)/gi; 
			if(!val.match(reg)){
				errors.push("Adresa de email este incorecta");
			}
		}
		if($("#comment-message").val() == ""){
			errors.push("Mesajul este obligatoriu");
		}
		if($("#comment-captcha").val() == ""){
			errors.push("Codul de verificare este obligatoriu");
		} else {
			$.ajax({
				url: baseURL+"comments_manager/ajax_check_captcha",
				type: "POST",
				data: {
					input: $("#comment-captcha").val(),
					item_id: $("form.comment input[type=hidden]").val()
				},
				async: false,
				dataType: "json",
				success: function(data){
					if(data.valid < 1){
						errors.push("Codul de verificare nu este corect");
					}
				}
			});
		}
		return errors;
	},
	refresh_comments : function(obj){
		comments_utils.current_page = parseInt(obj.page);
		var item_id = $("form.comment input[type=hidden]").val();
		if(comments_utils.current_page < 1){
			comments_utils.page = 1
		}
		$.ajax({
			url: baseURL+"comments_manager/ajax_get",
			type: "POST",
			data: {
				page:comments_utils.current_page,
				item_id: item_id
			},
			beforeSend:function(){
				$("ul.comments").html('<img src="'+baseURL+'images/ajax-loader.gif" class="loader" />');
			},
			success: function(data){
				$("ul.comments").html(data);
			}
		});
		$.ajax({
			url: baseURL+"comments_manager/ajax_paginate",
			type: "POST",
			data:{
				page:comments_utils.current_page,
				item_id: item_id
			},
			success: function(data){
				var el = $("div.pagesNav");
				el.before(data);
				el.remove();
				$("div.boxComments div.pagesNav a").click(comments_utils.pagination_events);
			}
		});
	},
	refresh_captcha: function(img){
		$.ajax({
			url: baseURL+"comments_manager/ajax_refresh_captcha",
			type: "POST",
			data: {
				item_id: $("form.comment input[type=hidden]").val()
			},
			success: function(data){
				img.attr("src",data);
			}
		});
	}
}

