$(document).ready(function() {

	function getCSec(sc) {
		if(!sc)
			return false;
		switch (sc) {
		case 'vehicles':
			return 'SL_SID_1';
		case 'realestate':
			return 'SL_SID_2';
		case 'jobs':
			return 'SL_SID_3';
		default:
			return '';
		}
		return false;
	}
	
	function incNotification(){
		var notification_tag = $("#saved_list_counter");
		var notification_span = $("#saved_list_counter strong.number");
		if(!notification_span)
			return false ;
		
		var notification_val = notification_span.text();
		try{
			if(notification_val>=0){
				notification_val++ ;
				notification_span.text(notification_val);
				if (notification_val==1) {
					$("#saved_list_container").css('display','block');
					return true;
				}
			}
		}
		catch(e){
			//notification_val = 0 ;
			return false ;
		}
	}
		
	
	function decNotification(){
		var notification_tag = $("#saved_list_counter");
		var notification_span = $("#saved_list_counter strong.number");
		if(!notification_span)
			return false ;
		
		var notification_val = notification_span.text();
		try{
			if(notification_val>=1){
				notification_val-- ;
				notification_span.text(notification_val);
				if (notification_val==0) {
					$("#saved_list_container").css('display','none');
					return true;
				}
			}
		}
		catch(e){
			//notification_val = 0 ;
			return false ;
		}
	}
	

	function save_rem(savetag) {
		var id = savetag.attr('id').replace(/^u?lst_/, "");
		var opt = /^u/.test(savetag.attr('id'));
		var sec = savetag.attr('rel');
		if( sec=='www'){
			try{
				if(corporate_section.length>1){
					sec = corporate_section;
				}
			}
			catch(e){
				return '' ;
			}
		}
		if (savetag.hasClass('selected')) {
			if (remList(id, sec, opt)){
				var all_tags = $('[id="'+savetag.attr('id')+'"]'); 
				all_tags.each(function(){
					$(this).removeClass('selected');
					var text_tag = $(this).find("em:first");
					if(text_tag.length==0){
						$(this).attr('title','שמור');
					}
					else{
						text_tag.text("שמור");
					}
				});
				decNotification();
			}
			return ;
		} 
		else {
			if (saveList(id, sec, opt)){
				var all_tags = $('[id="'+savetag.attr('id')+'"]'); 
				all_tags.each(function(){
					$(this).addClass('selected');
					var text_tag = $(this).find("em:first");
					if(text_tag.length==0){
						$(this).attr('title','מודעה נשמרה');
					}
					else{
						text_tag.text("מודעה נשמרה");
					}
				});
				incNotification();
			}
			return ;
		}

	}

	function saveList(id, sec, opt) {
		var cs = getCSec(sec);
		if(!cs)
			return false; 
		// write to cookie
		var new_flag = false ;
		var cur_c = readCookie(cs);
		cur_c = ( !cur_c ? '' : unescape(cur_c) );
		var reg = new RegExp( '(^|\|)'+id+'($|\|)' );
		if(!reg.test(cur_c)){
			//already exists
			var new_c = cur_c+'|'+id ;
			new_c = new_c.replace(/^\|/,'') ;
			createCrossDomainCookie(cs,new_c,365);
			new_flag = true ;
		}
		
		// send to server
		if(opt){
			updateUsrLst(id,sec,1);
		}

		return new_flag ;
	}

	function remList(id, sec, opt) {
		var cs = getCSec(sec);
		if(!cs)
			return false; 
		// write to cookie
		var new_flag = false ;
		var cur_c = readCookie(cs);
		cur_c = ( !cur_c ? '' : unescape(cur_c) );
		var reg = new RegExp( '(^|\|)'+id+'($|\|)' );
		if(reg.test(cur_c)){
			//already exists
			var new_c = cur_c.replace(reg,'|');
			new_c = new_c.replace(/\|+/g,'|').replace(/(^\|)|(\|$)/g,'');
			createCrossDomainCookie(cs,new_c,365);
			new_flag = true ;
		}
		
		// send to server
		if(opt){
			updateUsrLst(id,sec,0);
		}

		return new_flag ;
	}


	function updateUsrLst(id,sec,opt){

		if(/[^\d]/.test(id) ){
			return 'not numeric';
		}
		if(!getCSec(sec)){
			return false ;
		}
		if(opt!=0 && opt!=1){
			return false ;
		}

		$.ajax({
			type: 'POST',
			url: '/myuserdo/update_usr_lst/',
			dataType: 'json',
			data: {
			'id':id,
			'sec' :sec,
			'auth_val': Math.floor(Math.random()*100),
			'opt' : opt
			},
			success: function(json){
				if (json=='request succeed') {
					return true;
				}
				else{
					return false;
				}
			}
		});
		return false;
	}
	
	
	 function save_rem_map(savetag) {
		var id = savetag.attr('id').replace(/^u?lst_/, "");
		var opt = /^u/.test(savetag.attr('id'));
		var sec = 'realestate' ;

		if (savetag.hasClass('selected')) {
			if (remList(id, sec, opt)){
				var all_tags = $('[id="'+savetag.attr('id')+'"]'); 
				all_tags.each(function(){
					$(this).removeClass('selected');
					var text_tag = $(this).find("em:first");
					if(text_tag.length==0){
						$(this).attr('title','שמור');
					}
					else{
						text_tag.text("שמור");
					}
				});
				decNotification();
				//update global object
				update_saved_list(id,-1);
			}
			return ;
		} 
		else {
			if (saveList(id, sec, opt)){
				var all_tags = $('[id="'+savetag.attr('id')+'"]'); 
				all_tags.each(function(){
					$(this).addClass('selected');
					var text_tag = $(this).find("em:first");
					if(text_tag.length==0){
						$(this).attr('title','מודעה נשמרה');
					}
					else{
						text_tag.text("מודעה נשמרה");
					}
				});
				incNotification();
				//update global object
				update_saved_list(id,1);
			}
			return ;
		}

	}
	
	
	$("ul li a[id^='lst_'][rel],ul li a[id^='ulst_'][rel], table.l_list_table tr td a[id^='lst_'][rel],table.l_list_table tr td a[id^='ulst_'][rel]")
	.click(function() {
		save_rem($(this));
	});


	/*$("div.l_res_cnt_onmap ul li a[id^='lst_'][rel]," +
			"div.l_res_cnt_onmap ul li a[id^='ulst_'][rel]," +
			"table.l_list_table_map tr.l_res_cnt td div.click_saved_cnt a[id^='lst_'][rel]," +
			"table.l_list_table_map tr.l_res_cnt td div.click_saved_cnt a[id^='ulst_'][rel]")
	.live('click',function() {
		save_rem_map($(this));
	});*/

});
