/**
 * 
 * jQuery helper
 */

function ShowPopup(template) {

	// Fade out background
	jQuery(".wrap").animate({
	    opacity: 0.1
		
	}, 500);
	
	// Display the popup dialog
	jQuery.post(template, function(data) {
		jQuery('body').append('	<div class="wrap"> </div><div class="framework_Popup">'+data+'</div>');
	});

}

function SendActivateMail(mail) {
	var post_email = jQuery("#"+mail).val();
	
	// Remove the popup dialog
	jQuery('.framework_Popup').remove();
	
	ShowPopup('/catalog/view/theme/default/template/popup/logging_in.html');
	

	// Display the popup dialog
	jQuery.post('/index.php?route=account/sendactivate', { email: post_email }, function(data) {
		ShowPopup('/catalog/view/theme/default/template/popup/activate_sent.html');
	});
	
}

function NewPassword(mail) {

	var post_email = jQuery("#"+mail).val();
	
	// Remove the popup dialog
	jQuery('.framework_Popup').remove();
	
	ShowPopup('/catalog/view/theme/default/template/popup/logging_in.html');
	

	// Display the popup dialog
	jQuery.post('/index.php?route=account/ajaxpassword', { email: post_email }, function(data) {
		if(data == '0') {
			ShowPopup('/catalog/view/theme/default/template/popup/password_sent.html');
		} else {
			ShowPopup('/catalog/view/theme/default/template/popup/email_doesntexist.html');
		}
	});
}

function ShowPopupOK() {
	
	// Fade in Background
	jQuery(".wrap").animate({
	    opacity: 0.1
	}, 500);
	
	jQuery(".wrap").css("display","none");
	
	
	// Remove the popup dialog
	jQuery('.framework_Popup').remove();
}

function ShowPopupCancel() {
	
	// Fade in Background
	jQuery("body").animate({
	    opacity: 0.1
	}, 500);
	
	jQuery(".wrap").css("display","none");
	
	// Remove the popup dialog
	jQuery('.framework_Popup').remove();
}

function ShowAgent() {
	
	jQuery('html, body').animate({scrollTop:0}, 'slow');
	
	jQuery('.framework_Popup').remove();
	
	jQuery(".wrap").animate({
	    opacity: 0.1
	}, 500);
	
	var min_price = jQuery("#min_price").val();
	var max_price = jQuery("#max_price").val();
	var min_areal = jQuery("#areal_min").val();
	var max_areal = jQuery("#areal_max").val();
	var min_buildyear = jQuery("#buildyear_min").val();
	var max_buildyear = jQuery("#buildyear_max").val();
	var min_rooms = jQuery("#rooms_min").val();
	var max_rooms = jQuery("#rooms_max").val();
	
	// Fade out background
	jQuery(".wrap").animate({
	    opacity: 0.1
		
	}, 500);
	
	ShowPopup('/catalog/view/theme/default/template/popup/logging_in.html');
	
	// Display the popup dialog
	jQuery.post('/index.php?route=product/agent', { min_price: min_price, max_price: max_price, min_areal: min_areal, max_areal: max_areal, min_buildyear: min_buildyear, max_buildyear: max_buildyear, min_rooms: min_rooms, max_rooms: max_rooms } , function(data) {
		jQuery('body').append('	<div class="wrap"> </div><div class="framework_Popup">'+data+'</div>');
		jQuery('.framework_Popup').css('top','8%');
	});
}



function AgentSend() {
	jQuery('html, body').animate({scrollTop:0}, 'slow');
	
	var email_address = jQuery("#email_address").val();
	var maincats = jQuery("#maincat2_hidden").val();
	var subcats = jQuery("#subcat2_hidden").val();
	var types = jQuery("#types2_hidden").val();
	var regions = jQuery("#region2_hidden").val();
	var zipcodes = jQuery("#zipcodes2_hidden").val();
	var country = jQuery("#country2_select2 option:selected").val();
	
	var min_price = jQuery("#agent_min_price2").val();
	var max_price = jQuery("#agent_max_price2").val();
	var min_areal = jQuery("#agent_areal_min2").val();
	var max_areal = jQuery("#agent_areal_max2").val();
	var min_buildyear = jQuery("#agent_buildyear_min2").val();
	var max_buildyear = jQuery("#agent_buildyear_max2").val();
	var min_rooms = jQuery("#agent_rooms_min2").val();
	var max_rooms = jQuery("#agent_rooms_max2").val();

	jQuery('.framework_Popup').remove();
	
	jQuery(".wrap").animate({
	    opacity: 0.1
	}, 500);
	
	// Fade out background
	jQuery(".wrap").animate({
	    opacity: 0.1
		
	}, 500);
	
	ShowPopup('/catalog/view/theme/default/template/popup/logging_in.html');
	
	// Display the popup dialog
	jQuery.post('/index.php?route=product/agent/sendPost', { email_address: email_address, maincats: maincats, subcats: subcats, types: types, regions: regions, min_price: min_price, max_price: max_price, min_areal: min_areal, max_areal: max_areal, min_buildyear: min_buildyear, max_buildyear: max_buildyear, min_rooms: min_rooms, max_rooms: max_rooms, zipcodes: zipcodes, country_id: country } , function(data) {
		ShowPopup('/catalog/view/theme/default/template/popup/agent_ok.html');
	});
}

function onEnterInputText(e) {
	
	// Reset input value
	jQuery(e).val('');
}

function onExitInputText(e) {
	
	// Reset input value if empty
	if(jQuery(e).val() == '') {
		var val = jQuery(e).attr('title');
		jQuery(e).val(val);
	}
}

function getVal(id) {
	return jQuery(id).val();
}

function front_Login() {
	
	jQuery('.framework_Popup').remove();
	
	jQuery(".wrap").animate({
	    opacity: 0.1
	}, 500);
	
	ShowPopup('/catalog/view/theme/default/template/popup/logging_in.html');
	
	var user = jQuery("#login_username").val();
	var pass = jQuery("#login_password").val();
	
	jQuery.post('/index.php?route=module/loginbox/login', { username: user, password: pass }, function(data) {
		if(data == 1) {
			jQuery('.framework_Popup').remove();
			ShowPopup('/catalog/view/theme/default/template/popup/wrong_password.html');
		} else if (data == 2) {
			jQuery('.framework_Popup').remove();
			ShowPopup('/catalog/view/theme/default/template/popup/account_not_activated.html');
		} else {
			location.href = '/';
		}
	});
}

function addToFav(product_id) {
	
	// Display the popup dialog
	jQuery('.to_fav').css('display','none');
	jQuery('.favTxt').html('<img src="/catalog/view/theme/default/image/favAjax.gif" alt="" />');
	jQuery.post('/index.php?route=account/favourite/add', {product: product_id }, function(data) {
		jQuery('.favTxt').html('');
		jQuery('.to_fav').fadeIn('slow');
		jQuery('.to_fav').html('<input class="button" type="button" onclick="javascript:delFav(\''+product_id+'\');"  value="- Fjern fra favoritter" />');
	});
}

function delFav(product_id) {

	// Display the popup dialog
	jQuery('.to_fav').css('display','none');
	jQuery('.favTxt').html('<img src="/catalog/view/theme/default/image/favAjax.gif" alt="" />');
	jQuery.post('/index.php?route=account/favourite/delete', {product: product_id }, function(data) {
		jQuery('.favTxt').html('');
		jQuery('.to_fav').fadeIn('slow');
		jQuery('.to_fav').html('<input class="button" type="button" onclick="javascript:addToFav(\''+product_id+'\');"  value="+ Føj til favoritter" />');
	});
}

function openDiv(current, open) {
	jQuery("#"+open).css("top","0px");
	jQuery("#"+current).slideUp('slow', function() {
		jQuery("#"+current).css("display","none");
		jQuery("#"+open).slideDown('slow');
	});
}

