﻿// Adam Chetverkin © 2010
// mr.awers(at)gmail(dot)com
var loading_content = '<div class="loading_spinner"><img src="'+BASE_URL+'images/spinner.gif" /></div>';
// Get static page content action
function getStaticPage(name){
	$('#container').html(loading_content);
	$('#head_menu .menu_item').removeClass('current').removeClass('selected');
	$('.menu_btn_'+name).parent().addClass('current').addClass('selected');
	$.post(BASE_URL+"cms/page",{ alias: name }, function(data){
		$('#container').html(data);
	});	
	return false;
}

google.load("language", "1");
function newWindow (mypage,myname,w,h,features) {
    if(screen.width) {
          var winl = (screen.width-w)/2;
          var wint = (screen.height-h)/2;
      } else {
          winl = 0;wint =0;
      }

      if (winl < 0) winl = 0;
      if (wint < 0) wint = 0;

      var settings = 'height=' + h + ',';
      settings += 'width=' + w + ',';
      settings += 'top=' + wint + ',';
      settings += 'left=' + winl + ',';
      settings += features;
      settings += ' scrollbars=yes ';

      win = window.open(mypage,myname,settings);
      win.window.focus();
}

// Google Translator initialize function
function translator_initialize() {
	$('.totranslate').each(function(){
		var totranslate = this;
		// Exception for the "cn" language
		if (GLOBAL_LANGUAGE == 'cn'){
			lang = 'zh-TW';
		}else{
			lang = GLOBAL_LANGUAGE;
		}
		var html = $(totranslate).html();
		if(html.length >= 1400){
			return true;
		}
		google.language.translate(html, "en", lang, function(result) {
			if (!result.error) {
				$(totranslate).html(result.translation);
				google.language.getBranding(totranslate);
			}
		});
	});
}


// Get listings via ajax
function getListings(use_options, page){
	// Show loading spinner
	$('#container').html(loading_content);

	// Do ajax request
	options = use_options?$('#search_box_form').serialize():'';	
	if (page) options += '&page='+page;
	$.post(BASE_URL+"advertisement/list",options, function(data){
		// Show loaded data
		$('#container').html(data);
        if(options != '')
            document.location.hash = '/'+options;
		// Fix listings links - open all links in the new blank page.
		$('.listing a').each(function(){ if(this.href){ this.target='_blank';  } });

		// Translator
		if (GLOBAL_LANGUAGE != "en"){
			translator_initialize();
		}
	});
	return false;
}








$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('#') + 2).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});




function getListingsFromHash(hash){
    var hash = hash.replace('#/', '');
	$.post(BASE_URL+"advertisement/list", hash, function(data){
		// Show loaded data
		$('#container').html(data);

        // Category
        $('#search_box_form .category input[value="'+$.getUrlVar('category')+'"]').attr('checked', 'checked');
        // Keywords
        $('#search_box_form input#keyw').val($.getUrlVar('keywords'));
        // From
        $('#search_box_form input.from[value="'+$.getUrlVar('from')+'"]').attr('checked', 'checked');
        // Country
        $('#search_box_form #nation option[value="'+$.getUrlVar('country')+'"]').attr('selected', 'selected');
        // State
        $('#search_box_form #province option[value="'+$.getUrlVar('state')+'"]').attr('selected', 'selected');
        // Meta
        $('#search_box_form #majormeta option[value="'+$.getUrlVar('meta')+'"]').attr('selected', 'selected');
        // Near
        $('#search_box_form input#address').val($.getUrlVar('near'));

		// Fix listings links - open all links in the new blank page.
		$('.listing a').each(function(){ if(this.href){ this.target='_blank';  } });

		// Translator
		if (GLOBAL_LANGUAGE != "en") {
			translator_initialize();
		}
	});
	return false;
}


// Show listing long description instead of short description
function showDescription(listing_id){
	$('#listing_'+listing_id+'_description_short').hide();
	$('#listing_'+listing_id+'_description').show();
	return false;
}

// On "document ready" action
$(document).ready(function(){
		// login box behaviours
		$('#login_form_login').blur(function(){ if ($(this).val() == '')  $(this).val('Username'); });
		$('#login_form_login').focus(function(){ if ($(this).val() == 'Username') $(this).val(''); });

		$('#login_form_password').blur(function(){ if ($(this).val() == '')  $(this).val('Password'); });
		$('#login_form_password').focus(function(){ if ($(this).val() == 'Password') $(this).val(''); });


		// menu behaviours
		$('.menu_btn_terms').click(function(){			
			getStaticPage('terms');
			return true;
		});

		$('.menu_btn_about').click(function(){
			getStaticPage('about');
			return true;
		});

		$('.menu_btn_post').click(function(){
			getStaticPage('post');	
			return true;
		});

		$('.menu_btn_contact').click(function(){
			getStaticPage('contact');
			return true;
		});

		$('#head_menu .menu_item').mouseover(function(){ if(!$(this).hasClass('current')){ $(this).addClass('selected'); } });
		$('#head_menu .menu_item').mouseout(function(){ if(!$(this).hasClass('current')){ $(this).removeClass('selected'); } });






		$('#search_box_form').submit(function(event){
            event.preventDefault();
			$('#use_pricing').attr('value', '1');
			getListings(true);
            return false;
        });

		// Autoreload listings on following events:
		$('#search_box_form input[type=radio]').change(function(){
			$('#google_map').hide();
			$('#use_pricing').attr('value', '1');
			//getListings(true);
		});
		$('#search_box_form select').change(function(){
			$('#use_pricing').attr('value', '1');
			//getListings(true);
		});
		$('#search_box_form input[type=text]').keyup(function(){			
			$('#use_pricing').attr('value', '1');
			//getListings(true);
		});
		$('#locally').unbind();
		$('#address').unbind();
		$('#locally').focus(function(){
			if($('#address').val()!='')
			{
				$('#google_map').show();
				codeAddress($('#address'), $('#Latitude'), $('#Longitude'), $('#google_map'), $('#map_canvas')[0], function(){/*getListings(true);*/});
			}						
		});
				
		$('#nation').focus(function(){	
			$('#google_map').hide();
			$('#nationwide').attr('checked', true);
		});
		
		$('#province').focus(function(){
			$('#google_map').hide();
			$('#state').attr('checked', true);
		});
		
		$('#majormeta').focus(function(){
			$('#google_map').hide();
			$('#metarea').attr('checked', true);
		});
		
		$('#address').focus(function(){	
			$('#locally').attr('checked', true);
			if($('#address').val()!='')
			{
				//getListings(true);
				$('#google_map').show();
				codeAddress($('#address'), $('#Latitude'), $('#Longitude'), $('#google_map'), $('#map_canvas')[0], function(){/*getListings(true);*/});
			}
		});
		
		$('#address').change(function(){
			setTimeout(function(){
				if($('#locally').attr('checked'))
				{
					codeAddress($('#address'), $('#Latitude'), $('#Longitude'), $('#google_map'), $('#map_canvas')[0], function(){/*getListings(true);*/});
				}
				}, 200);
			});
});
function showVideo(url){
	videoId = url.slice(url.indexOf('v=')+2,url.length);
	window.open(BASE_URL+'index/player#'+videoId,'_blank','width=455,height=425, left=200, top=100, scrollbars=no'); 
}

