var iHaloAdv = function(){
	
    return {
    	
    	conditions		 : new Array(),
    	conditions_names : new Array(),
    	IDS	  	  		 : '',
    	
    	kiemel : function(advertiseId) {
	    	$.ajax({
	    		type: "POST",
	    		url: "/adatlap/kiemel/"+advertiseId,
	    		success: function(msg){
	    			alert(msg);
	    		}
	    	});
    	},
    	
        set_values : function(items) {
        	var values = items.split(',');
        	for (i = 0; i < values.length; i++) {
        		var item = values[i].split('#');
        		$("#selected_ad").append("<option value='"+item[1]+"'>"+item[0].replace('_', ',')+"</option>");
        		this.conditions.push(item[1]);
        		var data = item[2]+"#"+item[1]+"#"+item[0];
        		this.conditions_names.push(data);
        	}
        },
    	
        openAdvertise : function(advertise_id) {
    		var next_id = '';
    		var prev_id = '';
    		if (iHaloAdv.IDS != '') {
        		for (i = 0; i < iHaloAdv.IDS.length; i++) {
        			if (iHaloAdv.IDS[i] == advertise_id) {
        				if (iHaloAdv.IDS[i-1]) {
        					prev_id = iHaloAdv.IDS[i-1];
        				}
        				if (iHaloAdv.IDS[i+1]) {
        					next_id = iHaloAdv.IDS[i+1];
        				}
        			}
        		}
    		}
	    	$.ajax({
	    		type: "POST",
	    		url: "/adatlap/getadvertise/"+advertise_id,
	    		data: {
	    			next_id : next_id,
	    			prev_id : prev_id
	    		},
	    		success: function(msg){
	    			$('#detaileddata_preview').html(msg);
	    	    	$("div#detaileddata_preview").dialog({
	    	    		width: 940,
	    	    		height: 611,
	    	    		resizable: false,
	    	    		modal: !$.browser.msie,
	    	    		closeOnEscape: true,
	    	    		zIndex: 10
	    	    	});
	    	    	$("a.group").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true });
	    		}
	    	});
        },
        
        saveAdvertise2 : function(advertise_id) {
        	var ajaxurl = "/adatlap/saveadvertise2/";
        	
	    	if ($('#detaileddata_address').val() != '' &&
	        	$('#detaileddata_address').val() != 'itt adhat meg egy szabadon választott címet' &&
	        	$('#detaileddata_price').val() != '' &&
	        	$('#detaileddata_description').val() != '') {
	    		
	    		if (iHalo.isNumeric($('#detaileddata_price').val())) {
	    			if (iHaloAdv.conditions.length > 0) {
	    				$("input#hirdfeladbutton").css('display', 'none');
				    	$.ajax({
				    		type: "POST",
				    		url: ajaxurl,
				    		data: ({
				    			title: $('#detaileddata_address').val(),
				    			price: $('#detaileddata_price').val(),
				    			price_type: $('#price_type').val(),
				    			description: $('#detaileddata_description').val(),
				    			cim: $('#cim').val(),
				    			siteid: $('#siteid').val(),
				    			conditions: iHaloAdv.conditions.toString(),
				    			defaultImage : $('#defaultImage:checked').val(),
				    			
				    			registration_contact_name : $('#registration_contact_name').val(),
				    			registration_client_telephone1 : $('#registration_client_telephone1').val(),
				    			registration_client_telephone2 : $('#registration_client_telephone2').val(),
				    			registration_client_telephone3 : $('#registration_client_telephone3').val(),
				    			registration_client_email : $('#registration_client_email').val(),
				    			registration_client_zipcode : $('#registration_client_zipcode').val(),
				    			registration_client_city : $('#registration_client_city').val(),
				    			registration_client_street : $('#registration_client_street').val(),
				    			registration_client_www : $('#registration_client_www').val()
				    		}),
				    		success: function(msg){
				    			if (msg == 'wrongemail') {
				    				alert('A megadott e-mail címmel már létezik regisztráció!');
				    			} else {
					    			if (msg == 'success') {
					    				alert('A hirdetés feladása sikeres!');
					    				location.href='/kereses/';
					    			} else {
					    				$("input#hirdfeladbutton").css('display', 'block');
					    				alert('A hirdetés feladása sikertelen! Kérjük próbálja meg később!');
					    			}
				    			}
				    		}
				    	});
	    			} else {
	    				alert('Nem adott meg egyetlen feltételt sem!');
	    			}
	    		} else {
	    			alert('Az ár mezőben csak számot adhat meg!');
	    		}
	    		
	    	} else {
	    		alert('A csillaggal jelölt mezők kitöltése kötelező!');
	    	}
        },
        
    	saveAdvertise : function(advertise_id) {
        	
        	if (advertise_id) {
        		var ajaxurl = "/adatlap/modifyadvertise/"+advertise_id;
        	} else {
        		var ajaxurl = "/adatlap/saveadvertise/";
        	}
	    	if ($('#detaileddata_address').val() != '' &&
	        	$('#detaileddata_address').val() != 'itt adhat meg egy szabadon választott címet' &&
	        	$('#detaileddata_price').val() != '' &&
	        	$('#detaileddata_description').val() != '') {
	    		
	    		if (iHalo.isNumeric($('#detaileddata_price').val())) {
	    			if (iHaloAdv.conditions.length > 0) {
	    				$("input#hirdfeladbutton").css('display', 'block');//modified
				    	$.ajax({
				    		type: "POST",
				    		url: ajaxurl,
				    		data: ({
				    			title: $('#detaileddata_address').val(),
				    			price: $('#detaileddata_price').val(),
				    			price_type: $('#price_type').val(),
				    			description: $('#detaileddata_description').val(),
				    			cim: $('#cim').val(),
				    			siteid: $('#siteid').val(),
				    			conditions: iHaloAdv.conditions.toString(),
				    			defaultImage : $('#defaultImage:checked').val()
				    		}),
				    		success: function(msg){
				    			if (advertise_id) {
					    			if (msg == 'success') {
					    				alert('A hirdetés módosítása sikeres!');
					    				location.href='/adatlap/modositas/'+advertise_id;
					    			} else if (msg == 'unsuccess') {
					    				$("input#hirdfeladbutton").css('display', 'block');
					    				alert('A hirdetés módosítása sikertelen! Kérjük próbálja meg később!');
					    			} else {
					    				alert(msg);
					    			}
				    			} else {
					    			if (msg == 'success') {
					    				alert('A hirdetés feladása sikeres!');
					    				location.href='/adatlap/feltoltes/';
					    			} else if (msg == 'unsuccess') {
					    				$("input#hirdfeladbutton").css('display', 'block');
					    				alert('A hirdetés feladása sikertelen! Kérjük próbálja meg később!');
					    			} else {
					    				alert(msg);
					    			}
				    			}
				    		}
				    	});
	    			} else {
	    				alert('Nem adott meg egyetlen feltételt sem!');
	    			}
	    		} else {
	    			alert('Az ár mezőben csak számot adhat meg!');
	    		}
	    		
	    	} else {
	    		alert('A csillaggal jelölt mezők kitöltése kötelező!');
	    	}
    	},
    	
        closePreview : function() {
        	$("div#detaileddata_preview").dialog("destroy");
        },
        
        openPreview : function() {
        	if ($('#detaileddata_address').val() != '' &&
        		$('#detaileddata_address').val() != 'itt adhat meg egy szabadon választott címet' &&
        		$('#detaileddata_price').val() != '' &&
        		$('#detaileddata_description').val() != '') {
        		
        		if (iHalo.isNumeric($('#detaileddata_price').val())) {
        			if (iHaloAdv.conditions.length > 0) {
                    	$.ajax({
                    		type: "POST",
                    		url: "/adatlap/getpreview/",
                    		data: ({
                    			title: $('#detaileddata_address').val(),
                    			price: $('#detaileddata_price').val(),
                    			price_type: $('#price_type').val(),
                    			description: $('#detaileddata_description').val(),
                    			siteid: $('#siteid').val(),
                    			advertise_id: $('#advertise_id').val(),
                    			conditions: iHaloAdv.conditions.toString()
                    		}),
                    		success: function(msg){
                    			$('#detaileddata_preview').html(msg);
                            	$("div#detaileddata_preview").dialog({
            	    	    		width: 940,
            	    	    		height: 611,
            	    	    		resizable: false,
            	    	    		modal: true,
            	    	    		closeOnEscape: true,
            	    	    		zIndex: 10
                	        	});
                            	$("a.group").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true });
                    		}
                    	});          	
        			} else {
        				alert('Nem adott meg egyetlen feltételt sem!');
        			}
        		} else {
        			alert('Az ár mezőben csak számot adhat meg!');
        		}
        		
        	} else {
        		alert('A csillaggal jelölt mezők kitöltése kötelező!');
        	}
        },
    	
        back_value : function() {
        	if ($("#selected_ad :selected").val()) {
            	for (i = 0; i < iHaloAdv.conditions_names.length; i++) {
            		var splitted = iHaloAdv.conditions_names[i].split('#');
            		if (splitted[1] == $("#selected_ad :selected").val()) {
            			var value = $("#selected_ad :selected").val();
            			var value_splitted = splitted[2].split(': ');
            			var name = value_splitted[1];
            			/*
                		if ($("#required_ad :selected") && $("#required_ad :selected").val() == splitted[0]) {
                			$("#optional_ad").append("<option value='"+value+"'>"+name+"</option>");
                		}
                		*/
    	            	iHaloAdv.conditions = jQuery.grep(iHaloAdv.conditions, function(index) {
    	            		return index != value;
    	            	});
                		iHaloAdv.removeFromConditions_names(value);
        				if (splitted[0] == $("#required_ad :selected").val()) {
        					iHaloAdv.get_values($("#required_ad :selected").val());
        				}
                		$("#selected_ad :selected").remove();
            		}
            	}
        	}
        },
        
        sortOptions : function(domId) {
        	var $dd = $('#'+domId);
        	if ($dd.length > 0) { // make sure we found the select we were looking for

        	    // save the selected value
        	    var selectedVal = $dd.val();

        	    // get the options and loop through them
        	    var $options = $('option', $dd);
        	    var arrVals = [];
        	    $options.each(function(){
        	        // push each option value and text into an array
        	        arrVals.push({
        	            val: $(this).val(),
        	            text: $(this).text()
        	        });
        	    });

        	    // sort the array by the value (change val to text to sort by text instead)
        	    arrVals.sort(function(a, b){
        	        if(a.text>b.text){
        	            return 1;
        	        }
        	        else if (a.text==b.text){
        	            return 0;
        	        }
        	        else {
        	            return -1;
        	        }
        	    });


        	    // loop through the sorted array and set the text/values to the options
        	    for (var i = 0, l = arrVals.length; i < l; i++) {
        	        $($options[i]).val(arrVals[i].val).text(arrVals[i].text);
        	    }

        	    // set the selected value back
        	    $dd.val(selectedVal);
        	}

        },
        
        removeFromConditions_names : function(id) {
        	for (var i = 0; i < iHaloAdv.conditions_names.length; i++) {
    			var splitted = iHaloAdv.conditions_names[i].split('#');
    			if (splitted[1] == id) {
    				iHaloAdv.conditions_names = jQuery.grep(iHaloAdv.conditions_names, function(index) {
	            		return index != iHaloAdv.conditions_names[i];
	            	});
    			}
        	}
        },

        select_value : function() {
        	if ($("#required_ad :selected").val() && $("#optional_ad :selected").val()) {
        		this.conditions.push($("#optional_ad :selected").val());
        		$("#selected_ad").append("<option value='"+$("#optional_ad :selected").val()+"'>"+$("#required_ad :selected").text()+": "+$("#optional_ad :selected").text()+"</option>");
        		var data = $("#required_ad :selected").val()+"#"+$("#optional_ad :selected").val() +"#"+ $("#required_ad :selected").text().replace(',', '_')+": "+$("#optional_ad :selected").text().replace(',', '_');
        		this.conditions_names.push(data);
        		$("#optional_ad :selected").remove();
        	}
        },
    	
        quicksearch : function(word) {
        	if (!$("#required_ad :selected").val()) {
        		alert('Még nem választott feltételt!');
        	} else {
        		this.get_values($("#required_ad :selected").val(), $("#quicksearch_ad").val());
        	}
        },
    	
        get_values : function(cid) {
        	if (arguments[1]) {
        		var keyword = arguments[1];
        	} else {
        		var keyword = "";
        		$("#quicksearch_ad").val("gyorskereső");
        	}
        	
        	$.ajax({
        		type: "POST",
        		async: false,
        		url: "/adatlap/getvalues/" + cid + "/" + keyword,
        		data: ({selected : this.conditions.toString()}),
        		success: function(msg){
        			$('#optional_c_ad').html(msg);
        		}
        	});
        }
        
    };
    
}();