/*
* Listing view functions collection
* We use global var "id_listing" for detect current listing in functions
* use like smarty code for switching of listings
* <a href="#test" onclick="showPopup(this,'Popup title', 'popupContent'); id_listing={$id_listing}; return false">click me</a>
*/

// Initialization 

var id_listing = 0;

function set_id_listing(id) {
	if (typeof(id) == "number") {
		id_listing = id;
	} else if ($('id_listing') != null) // must be defined in template as hidden field
		id_listing = $('id_listing').value;
}
addLoadEvent(set_id_listing);

// --------------------------------------  LISTING INFO TABS ---------------
function switchListingTab(tab) {
    switch (tab) {
        case 1:{
            $("li_tab_1").innerHTML = '<span>Description</span>';
            $("li_tab_2").innerHTML = '<a href="javascript:switchListingTab(2);"><span>More Info</span></a>';
            $("li_tab_3").innerHTML = '<a href="javascript:switchListingTab(3);"><span>Amenities</span></a>';
            break;
        }
        case 2:{
            $("li_tab_1").innerHTML = '<a href="javascript:switchListingTab(1);"><span>Description</span></a>';
            $("li_tab_2").innerHTML = '<span>More Info</span>';
			$("li_tab_3").innerHTML = '<a href="javascript:switchListingTab(3);"><span>Amenities</span></a>';
            break;
        }
        case 3:{
            $("li_tab_1").innerHTML = '<a href="javascript:switchListingTab(1);"><span>Description</span></a>';
            $("li_tab_2").innerHTML = '<a href="javascript:switchListingTab(2);"><span>More Info</span></a>';
            $("li_tab_3").innerHTML = '<span>Amenities</span>';
			break;
        }
    }   
    ajax("get_listing_info.php?tab="+tab+'&id_listing='+id_listing, onLoadListingInfo);
}
//callback
function onLoadListingInfo(result) {
    $('info_content').innerHTML = result.str;
}
// ------------------------------------------- MAP TABS
function switchMapTab(tab) {
	// G_NORMAL_MAP This is the normal street map type. 
	// G_SATELLITE_MAP This map type shows Google Earth satellite images. 
	// G_HYBRID_MAP
	var mapFrame = window.frames['gmap_fr'];
	if (mapFrame.map == null) { alert('Please wait...'); return; }
	switch (tab) {
		case 1:
			mapFrame.map.setMapType(mapFrame.G_NORMAL_MAP);
			$('map_tab_1').innerHTML = '<span>Map</span>';
			$('map_tab_2').innerHTML = '<a href="javascript:switchMapTab(2)"><span>Satellite</span></a>';
			$('map_tab_3').innerHTML = '<a href="javascript:switchMapTab(3);"><span>Hybrid</span></a>';
			break;
		case 2:
			mapFrame.map.setMapType(mapFrame.G_SATELLITE_MAP);
			$('map_tab_1').innerHTML = '<a href="javascript:switchMapTab(1)"><span>Map</span></a>';
			$('map_tab_2').innerHTML = '<span>Satellite</span>';
			$('map_tab_3').innerHTML = '<a href="javascript:switchMapTab(3);"><span>Hybrid</span></a>';
			break;
		case 3:
			mapFrame.map.setMapType(mapFrame.G_HYBRID_MAP);
			$('map_tab_1').innerHTML = '<a href="javascript:switchMapTab(1)"><span>Map</span></a>';
			$('map_tab_2').innerHTML = '<a href="javascript:switchMapTab(2)"><span>Satellite</span></a>';
			$('map_tab_3').innerHTML = '<span>Hybrid</span>';
			break;
	}
}

// ------------------------------- WHAT ARE YOU???
function signup_whatareyou(id)
{
    layerHide('what_are_you_1');
    layerHide('what_are_you_2');
    layerHide('what_are_you_3');
    layerHide('what_are_you_4');
    if(id==1) layerShow('what_are_you_1');
    if(id==2) layerShow('what_are_you_2');
    if(id==3) layerShow('what_are_you_3');
    if(id==4) layerShow('what_are_you_4');
    //ajax("get_what_are_you.php?id="+id, on_get_what_are_you);
    //  $('div_what_are_you').innerHTML = id;
}

function on_get_what_are_you(result) {
    $('div_what_are_you').innerHTML = result.str;
}
// --------------------------------------- SEND VIDEO
function send_video(listing_id)
{
    var email_input = 'emails'+listing_id;
    //alert (email_input);
    emails=$(email_input).value;
    ajax("send_video.php?listing_id="+listing_id+"&emails="+emails, on_video_sent);
}

function on_video_sent(result)
{
    if (result.emails == "")
       $('emails'+result.id).value = "Addresses are not correct";
    else
       $('emails'+result.id).value = "Sent to: "+result.emails;
}

// -------------------------------------- POST VIDEO
function getWidgetSections(id) {
    var result = [];
    $('postVideoSections').innerHTML = "busy...click after few seconds";
    ajax("get_widget_sections.php?id="+id, onLoadSections);
}

function onLoadSections(result) {
    $('postVideoSections').innerHTML = result.str;
}

function postVideo(site,type)
{
	if ($("user").value == '' || $("pass").value == '') {
		if ($("user").value == '' && $("pass").value == '') 
			$('postSuccess').innerHTML = "Please enter username and password";
		else
			if ($("user").value == '')
				$('postSuccess').innerHTML = "Please enter username";
			else
				if ($("pass").value == '')
					$('postSuccess').innerHTML = "Please enter password";
		return ;
	}
    $('postSuccess').innerHTML = "Your widget is proceeding...";
	
    //flvideo = $("flvideo").value;
    user = $("user").value;
    password = $("pass").value;
	url = "curl/autologin_ajax.php?id_listing="+id_listing+"&site="+site+"&type="+type+"&user="+user+"&password="+password;
    ajax(url, onPostVideo);
}
function onPostVideo(result) {
    $('postSuccess').innerHTML = result.str;
}

// -----------------------------------  SHARE VIDEO
function shareVideo(){
    var url = 'ajax/share_video.php?id_listing='+id_listing+'&recipients='+$("recipients").value;
    url += '&first_name='+$("first_name").value+'&message='+$("message").value;
    ajax(url, onLoadShareVideo);   
}
function onLoadShareVideo(result) {
  if (result.err && result.err != '') {
    alert(result.err);
  } else if (result.msg && result.msg != '') {
    $('shareVideoMsg').innerHTML = result.msg;
	$('shareVideoBtn').value = ' OK ';
	$('shareVideoBtn').onclick = 'this.value = " Send "; this.onclick = "shareVideo();";';
  }
}
// --------------------------------------   COMMENTS USER TABS
var UserTab = 0; // logged user
function switchUserTab(tab) {
    var str = '';
    switch (tab){
        case 1:
			$('u_tab_1').innerHTML = '<span>New User</span>';
			$('u_tab_2').innerHTML = '<a href="javascript:switchUserTab(2)"><span>Existing User</span></a>';
			$('u_tab_3').innerHTML = '<a href="javascript:switchUserTab(3)"><span>Guest</span></a>';
            break;
        case 2:
			$('u_tab_1').innerHTML = '<a href="javascript:switchUserTab(1)"><span>New User</span></a>';
			$('u_tab_2').innerHTML = '<span>Existing User</span>';
			$('u_tab_3').innerHTML = '<a href="javascript:switchUserTab(3)"><span>Guest</span></a>';
            break;
        case 3:
			$('u_tab_1').innerHTML = '<a href="javascript:switchUserTab(1)"><span>New User</span></a>';
			$('u_tab_2').innerHTML = '<a href="javascript:switchUserTab(2)"><span>Existing User</span></a>';
			$('u_tab_3').innerHTML = '<span>Guest</span>';
            break;
    }
	UserTab = tab;
	for (var i = 1; i<=3; i++) $('frmU'+i).style.display = (i == tab) ? '' : 'none';
}
// --------------------------------------     COMMENTS
var CommentsType = 'A'; // all

function switchCommentsType(type) {
    var str = '';
    switch (type){
        case 'A':
			$('com_type_1').innerHTML = '<b>All</b>';
			$('com_type_2').innerHTML = '<a href="javascript:switchCommentsType(\'Q\')">Questions</a>';
			$('com_type_3').innerHTML = '<a href="javascript:switchCommentsType(\'C\')">Comments</a>';
            break;
        case 'Q':{
			$('com_type_1').innerHTML = '<a href="javascript:switchCommentsType(\'A\')">All</a>';
			$('com_type_2').innerHTML = '<b>Questions</b>';
			$('com_type_3').innerHTML = '<a href="javascript:switchCommentsType(\'C\')">Comments</a>';
            break;
        }
        case 'C':{
			$('com_type_1').innerHTML = '<a href="javascript:switchCommentsType(\'A\')">All</a>';
			$('com_type_2').innerHTML = '<a href="javascript:switchCommentsType(\'Q\')">Questions</a>';
			$('com_type_3').innerHTML = '<b>Comments</b>';
            break;
        }
    }
	ajax("view_video.php?comments_ajax=1&id_listing="+id_listing+"&comments_type="+type, 'comBlock'); // TransAjax
}
function addComments(type) {
    var c = $("com_body").value;
	/*
    if (c.replace(/^\s+|\s+$/g,'') == '') {
        alert('You must fill in the comment box!');
        return false;
    }
	*/
    var comment_body = escape(c);
	if (UserTab == 0) { // logged
    	ajax("view_video.php?id_listing="+id_listing+"&comments_ajax=1&body="+comment_body+"&type="+type, 'comBlock');
		$("com_body").value = '';
	} else if (UserTab == 1) { // new
		$('nu_com').value = comment_body;
		if (validator.checkForm('frmU1')) $('frmU1').submit();
	} else if (UserTab == 2) { // exist
		$('ex_com').value = comment_body;
		if (validator.checkForm('frmU2')) $('frmU2').submit();
	} else if (UserTab == 3) { // guest
		$('gt_com').value = comment_body;
		if (validator.checkForm('frmU3')) {
			var gt_name = $('gt_username').value;
			var gt_email = $('gt_email').value;
			ajax("view_video.php?id_listing="+id_listing+"&comments_ajax=1&body="+comment_body+"&type="+type+"&gt_name="+gt_name+"&gt_email="+gt_email, 'comBlock');
			$("com_body").value = '';
		}
	}
	
}

function deleteComments(comment_id)
{
    if (confirm('You want to delete this question?'))
        ajax("view_video.php?comments_ajax=1&delete=" + comment_id, 'comBlock');
}
function showReplyForm(comment_id) {
    $('comReplyLink' + comment_id).innerHTML = '<strong>Type your answer in the following box: </strong>';
    layerShow('comReplyForm' + comment_id);
}
function saveReply(comment_id) {
    var id_listing_info=$("id_listing_info").value;
    answer = escape($('comAnswer' + comment_id).value);
    ajax("view_video.php?comments_ajax=1&id_listing=" + id_listing_info + "&reply=" + comment_id + "&answer=" + answer, 'comBlock');
}
/*
function onLoadComments(result) {
    if (comment = $('txtComments'))
        comment.value = "";
    $('comBlock').innerHTML = result.str;
}
*/
//-------------------------------------------------   TAPING ???? ------------------

function edit_taping(tape_id) 
{
    ajax("taping.php?sess_uniqid=" + tape_id + "&action=edit&ajax=1", on_taping_edit);    
}

function delete_taping(tape_id) 
{
    if (confirm('You really want to delete this taping?'))
        ajax("taping.php?sess_uniqid=" + tape_id + "&action=delete&ajax=1", delete_from_cart);    
}

function delete_from_cart(result)
{
    ul = $('cart_' + result.deleted_id).parentNode;
    for (i = 0; i < ul.childNodes.length; i++) {
        if (ul.childNodes[i].id == 'cart_' + result.deleted_id) {
            ul.removeChild(ul.childNodes[i]);
            break;
        }
    }
    $('totalAmount').value = result.totalAmount;
}

function selectOption(id, current)
{
    var select = $(id);
    for (i = 0; i < select.options.length; i++) {
        if (select.options[i].value == current) { 
           select.selectedIndex = i; 
           break;
        }
    } 
}

function on_taping_edit(result)
{
    $("tp_address").value = result.address;
    $("tp_agent").value = result.agent;
    $("tp_cell").value = result.cell;
    $("tp_email").value = result.email;
    selectOption('tp_videotape', result.videotape);
    
    if (result.hires == 'no')
        $('tp_hiresno').checked = true;
    else
        $('tp_hiresyes').checked = true;
        
    selectOption('tp_month', result.month);
    selectOption('tp_day', result.day);
    selectOption('tp_time', result.time);
    $("tp_promcode").value = result.promcode;
    
    subm = $("tp_submit");
    subm.name = 'update_cart';
    subm.value = 'Update and Get Another Property Taped';
    
    $("tp_sess_uniqid").value = result.sess_uniqid;
}
//-----------------------------------------------  CONTACT
function contact(mode) {
	layerHide('contactError');
    var message = $("contact_message").value;
    var url = "ajax/view_video_contact.php?id_listing="+id_listing+"&mode="+mode+"&message="+escape(message);
    ajax(url, onCompleteContact);
}
function onCompleteContact(result) {
    if(result.success) {
		$("popupContact").innerHTML = '<div style="margin: 50px; text-align: center">'+result.message+'</div>';
    } else {
		layerShow('contactError');
    }
}
// ---------------------------------------------- SAVE TO FAVORITES
function saveToFav() {
	var url = 'ajax/save_to_fav.php?id_listing='+id_listing;
	ajax(url, onCompleteFav);
}
function onCompleteFav(result) {
	if (result.success) {
		$("msgSaveToFav").innerHTML = result.message;
	} else {
		$("msgSaveToFav").innerHTML = '<span style="color: red;">'+result.error+'</span>';
	}
}
// ------------------------------------------- FLAG LISTING
function flagVideo(flag) {
	for (var i=1; i<=3; i++) {
      if($("flag"+i).checked) {
        var flag = i;
		break;
      }   
    }
	var url = 'ajax/flag_video.php?id_listing='+id_listing+'&flag='+flag;
    ajax(url, onFlagVideo);
}
function onFlagVideo(result) {
	if (result.success) {
		$("msgFlagVideo").innerHTML = '<span style="color: green">'+result.message+'</span>';
	} else {
		$("msgFlagVideo").innerHTML = '<span style="color: red">'+result.error+'</span>';
	}
		
}
// ------------------------------------------- PROPERTY ALERTS
function savePA() {
    var sources = [];
    if($("chkAlertEmail").checked) sources.push('email');
    if($("chkAlertPhone").checked) sources.push('phone');
    if($("chkAlertRss").checked) sources.push('rss');

    var onCreate = $("chkOnCreate").checked ? 1:0;
    var onSold = $("chkOnSold").checked ? 1:0;
    var url = "ajax/property_alerts_ajax.php?id_listing="+id_listing+"&sources="+sources.join(",")+"&onCreate="+onCreate+"&onSold="+onSold;
    ajax(url, onCompletePA);
}
function onCompletePA(result) {
    $('popupPA').innerHTML=result.str;
}
function rssRegister(_target) {
	_target = _target.replace(/__url/, 'http%3A//reelproperties.tv/rss/properties/'+id_listing+'/');
	window.open(_target,'rss','');
	return true;
}
// -------------------------------------------- MOST VIEWED

function get_most_viewed_list(){
    ajax("get_most_poplist.php", on_get_mostviewed_listings);
}
function on_get_mostviewed_listings(result){
    byid("listing_1").innerHTML = "<a href='javascript:order_flistings(1);' style='color: #888;'>Featured Listings</a>";
    byid('side_results_listings').innerHTML = result.str;
}

function order_flistings(id_tab)
{
    ajax("ajax/get_listings.php?id_tab="+id_tab+'&id_listing_info='+id_listing_info, on_get_featured_listings);

    if(id_tab==1) {
        byid("listing_1").innerHTML = "<b>Featured Properties</b>";
    }
}

function on_get_featured_listings(result){
    $('side_results_listings').innerHTML = result.str;
}

on_get_listing_info_pane_number =''; // global


//-------------------------------------------------- BOOKMARKS
function addToFavorites(){
    var title = document.title;
    var url = location.href;

    if (window.sidebar) { // Mozilla Firefox Bookmark
        window.sidebar.addPanel(title, url,"");
    }
     else if( window.external ) { // IE Favorite
        window.external.AddFavorite( url, title); }
    else if(window.opera && window.print) { // Opera Hotlist
        return true;
	}
}
// Callback function to replace document url and titles with current
function addToSocial(_target) {
	_url = encodeURIComponent(document.location.href);
	_title = encodeURIComponent(document.title);
	_target = _target.replace(/__url/ig,_url);
	_target = _target.replace(/__title/ig,_title);
	window.open(_target,'social','');
}

//------------------------------------ ??????????????????????????????????????????
function getMoreDetails(id_listing){
 if($("tr_"+id_listing).style.display != "none"){
    $("tr_"+id_listing).style.display = "none";      
    $("updown_"+id_listing).src = "images/arr_down.png";  
    $("linkTitle_"+id_listing).innerHTML = "More details";
 }
 else{
    $("updown_"+id_listing).src = "images/arr_up.png";    
    $("linkTitle_"+id_listing).innerHTML = "Less details";   
    var url = 'ajax/property_info_ajax.php?action=getDetails&id_listing='+id_listing;
    ajax(url, on_getMoreDetails);       
 }
}

function on_getMoreDetails(result){
    $("tr_"+result.id_listing).style.display = "";   
    $("det_"+result.id_listing).innerHTML = result.str;  
}


// --------------------------------------- POPUP MOVE/RESIZE CONTROL

function setBounds(element, x, y, width, height,altPopup){
  if (typeof(element) == "string")
    element = $(element);
  if (element == null) alert('setBounds: not found'+element);
  var b = getBounds(element);
  if (typeof(x) == "undefined"){
    x = 0;
  }
 
  if (typeof(y) == "undefined"){
    y = 0;
  }

  if (typeof(width) == "undefined"){
    width = 377;
  }

  if (typeof(height) == "undefined"){
    height = 150;   
  }
  p = typeof(altPopup) == "undefined" ? 'popup_1' : altPopup;
  setLeft(p, b.left + x);  
  setTop(p, b.top + y);    
  setWidth(p, width);         
  setHeight(p, height);
}

//Get element's coordinate
function getBounds(element) {
  var p = getAbsolutePos(element,true); // see global.js
  return {left: p.x, top: p.y, width: element.offsetWidth, height: element.offsetHeight};
}

//Set element's coordinate
function setTop(id, y) {
   $(id).style.top = y+"px";
   //alert($(id).style.position);
}

function setLeft(id, x) {
  $(id).style.left = x+"px";
}

function setWidth(id, value) {
   $(id).style.width = value+"px";
}

function setHeight(id, value) {
   $(id).style.height = value+"px";
}