/*
 * This file has the functions needed to open up the
 * modal windows from the search results page
 */
var contentWin = null;
/*
 * This will pop up the showing request form
 */
function request_showing_window(lid, uid, page) {	
	var winDim = document.viewport.getDimensions();

	if(winDim.width < 830 || winDim.height < 600) {
	    var widthWindow = winDim.width - 200;
	    var heightWindow = winDim.height - 150;

	    widthWindow = (widthWindow < 800) ? winDim.width - 100 : widthWindow - 100;
	    heightWindow = (heightWindow < 600) ? winDim.height - 100 : heightWindow - 100;

	    //just in case getDimensions() doesn't work we want
	    //to manually set the height and width
	    widthWindow = (widthWindow) ? widthWindow : 600;
	    heightWindow = (heightWindow) ? heightWindow : 400;
	} else {
	    widthWindow = (Prototype.Browser.IE) ? 840 : 830;
	    heightWindow = 600;
	}

    var url = "/_Find-Homes/req-show1-layer.php?lid="+lid+"&aid="+uid;

    win = new Window({
        className: "alphacube",
        width:widthWindow,
        height:heightWindow,
        url: url,
        showEffectOptions:{duration:0.1}});

    win.show();
    win.showCenter('modal');
    
    //we need to track that the user clicked this button
	var url = "/includes/tracking.php?action=showing&uid="+uid+"&lid="+lid+"&page="+page;
	
	var myAjax = new Ajax.Request(url, {
        method: 'get',
        asynchronous: false
    });
}

/*
 * This will pop up the contact form
 */
function ask_questions_window(lid, mls_name, mls_id, uid, page) {
	var winDim = document.viewport.getDimensions();

	if(winDim.width < 830 || winDim.height < 600) {
	    var widthWindow = winDim.width - 200;
	    var heightWindow = winDim.height - 150;

	    widthWindow = (widthWindow < 800) ? winDim.width - 100 : widthWindow - 100;
	    heightWindow = (heightWindow < 600) ? winDim.height - 100 : heightWindow - 100;

	    //just in case getDimensions() doesn't work we want
	    //to manually set the height and width
	    widthWindow = (widthWindow) ? widthWindow : 600;
	    heightWindow = (heightWindow) ? heightWindow : 400;
	} else {
	    widthWindow = 830;
	    heightWindow = (Prototype.Browser.IE) ? 575 : 540;
	}

    var url = "/_Find-Homes/request-info-layer.php?lid="+lid+"&mls_name="+mls_name+"&mls_id="+mls_id+"&aid="+uid;

    win = new Window({
        className: "alphacube",
        width:widthWindow,
        height:heightWindow,
        url: url,
        showEffectOptions:{duration:0.1}});

    win.show();
    win.showCenter('modal');
    
    //we need to track that the user clicked this button
    if(lid == 0 && mls_name == 0 && mls_id == 0) {
    	//this means the user clicked on contact agent
    	var url = "/includes/tracking.php?action=contact&uid="+uid+"&page="+page;
    } else {
    	var url = "/includes/tracking.php?action=ask&uid="+uid+"&lid="+lid+"&page="+page;
    }
	
	var myAjax = new Ajax.Request(url, {
        method: 'get',
        asynchronous: false
    });
}

/*
 * This will pop up the map for a single listing
 */
function view_map(lid) {
	var winDim = document.viewport.getDimensions();

	if(winDim.width < 630 || winDim.height < 613) {
	    var widthWindow = winDim.width - 200;
	    var heightWindow = winDim.height - 150;

	    widthWindow = (widthWindow < 800) ? winDim.width - 100 : widthWindow - 100;
	    heightWindow = (heightWindow < 600) ? winDim.height - 100 : heightWindow - 100;

	    //just in case getDimensions() doesn't work we want
	    //to manually set the height and width
	    widthWindow = (widthWindow) ? widthWindow : 600;
	    heightWindow = (heightWindow) ? heightWindow : 400;
	} else {
	    widthWindow = 630;
	    heightWindow = (Prototype.Browser.IE) ? 642 : 614;
	}
	
    var url = "/listings/AtlasSearch.php?MappingMode=listing&temp=100&lid="+lid+"&popup=1";

    win = new Window({
        className: "alphacube",
        width:widthWindow,
        height:heightWindow,
        url: url,
        showEffectOptions:{duration:0.1}});

    win.show();
    win.showCenter('modal');
}

/*
 * This will pop up the form which emails a listing to a friend
 */
function email_listing_window(lid, mls_id) {
	var winDim = document.viewport.getDimensions();

	if(winDim.width < 830 || winDim.height < 600) {
	    var widthWindow = winDim.width - 200;
	    var heightWindow = winDim.height - 150;

	    widthWindow = (widthWindow < 800) ? winDim.width - 100 : widthWindow - 100;
	    heightWindow = (heightWindow < 600) ? winDim.height - 100 : heightWindow - 100;

	    //just in case getDimensions() doesn't work we want
	    //to manually set the height and width
	    widthWindow = (widthWindow) ? widthWindow : 600;
	    heightWindow = (heightWindow) ? heightWindow : 400;
	} else {
	    widthWindow = 830;
	    heightWindow = (Prototype.Browser.IE) ? 600 : 530;
	}

    var url = "/_Find-Homes/email-layer.php?lid="+lid+"&mls_id="+mls_id;

    win = new Window({
        className: "alphacube",
        width:widthWindow,
        height:heightWindow,
        url: url,
        showEffectOptions:{duration:0.1}});

    win.show();
    win.showCenter('modal');
}

/*
 * This will pop up the window with the different print flyers
 */
function print_flyer_window(lid) {
	var winDim = document.viewport.getDimensions();

	if(winDim.width < 830 || winDim.height < 600) {
	    var widthWindow = winDim.width - 200;
	    var heightWindow = winDim.height - 150;

	    widthWindow = (widthWindow < 800) ? winDim.width - 100 : widthWindow - 100;
	    heightWindow = (heightWindow < 600) ? winDim.height - 100 : heightWindow - 100;

	    //just in case getDimensions() doesn't work we want
	    //to manually set the height and width
	    widthWindow = (widthWindow) ? widthWindow : 600;
	    heightWindow = (heightWindow) ? heightWindow : 400;
	} else {
	    widthWindow = 830;
	    heightWindow = 540;
	}

    var url = "/_Find-Homes/print1-layer.php?lid="+lid;

    win = new Window({
        className: "alphacube",
        width:widthWindow,
        height:heightWindow,
        url: url,
        showEffectOptions:{duration:0.1}});

    win.show();
    win.showCenter('modal');
}
 
function view_open(lid, mls_id, cid) {
	var winDim = document.viewport.getDimensions();

	if(winDim.width < 830 || winDim.height < 600) {
	    var widthWindow = winDim.width - 200;
	    var heightWindow = winDim.height - 150;

	    widthWindow = (widthWindow < 800) ? winDim.width - 100 : widthWindow - 100;
	    heightWindow = (heightWindow < 600) ? winDim.height - 100 : heightWindow - 100;

	    //just in case getDimensions() doesn't work we want
	    //to manually set the height and width
	    widthWindow = (widthWindow) ? widthWindow : 600;
	    heightWindow = (heightWindow) ? heightWindow : 400;
	} else {
	    widthWindow = 830;
	    heightWindow = 540;
	}

    var url = "/_Find-Homes/OH-layer.php?&&hmlid="+lid+"&hmmls="+mls_id+"&companyid="+cid;

    win = new Window({
        className: "alphacube",
        width:widthWindow,
        height:heightWindow,
        url: url,
        showEffectOptions:{duration:0.1}});

    win.show();
    win.showCenter('modal');	
}

function openFeatured() {
    if (contentWin != null) {
        return false;
        //Dialog.alert("Close the window 'Test' before opening it again!",{width:200, height:130});
    } else {
        contentWin = new Window({
            className: "alphacube",
            maximizable: false,
            resizable: false,
            hideEffect:Element.hide,
            showEffect:Element.show,
            destroyOnClose: true
        });
        //$('featured-listings').setStyle({height:'270px',overflow:'hidden'});
        if (navigator.userAgent.indexOf('MSIE 6') != -1) {
            $('featured-listings').setStyle({height:'325px',overflow:'hidden'}); // only the listing section
            $('featured-results').setStyle({height:'382px',overflow:'hidden'}); // entire blue background
        } else if (navigator.userAgent.indexOf('MSIE 7') != -1) {
            $('featured-listings').setStyle({height:'325px',overflow:'hidden'}); // only the listing section
            $('featured-results').setStyle({height:'372px',overflow:'hidden'}); // entire blue background
        } else {
            $('featured-listings').setStyle({height:'325px',overflow:'hidden'}); // only the listing section
            $('featured-results').setStyle({height:'368px',overflow:'hidden'}); // entire blue background
        }
        $('view-all-link').hide();
        contentWin.setContent($('featured-results'), true, true)
        contentWin.show();
        contentWin.showCenter('modal');

        // Set up a windows observer, check ou debug window to get messages
        myObserver = {
            onDestroy: function(eventName, win) {
                if (win == contentWin) {
                    $('featured-results-main').appendChild($('featured-results'));
                    if (navigator.userAgent.indexOf('MSIE 6') != -1) {
                        $('featured-listings').setStyle({height:'155px',overflow:'hidden'}); // only the listing section
                        $('featured-results').setStyle({height:'206px',overflow:'hidden'}); // entire blue background
                    } else if(navigator.userAgent.indexOf('MSIE 7') != -1) {
                        $('featured-listings').setStyle({height:'155px',overflow:'hidden'}); // only the listing section
                        $('featured-results').setStyle({height:'200px',overflow:'hidden'}); // entire blue background
                    } else {
                        $('featured-listings').setStyle({height:'155px',overflow:'hidden'}); // only the listing section
                        $('featured-results').setStyle({height:'194px',overflow:'hidden'}); // entire blue background
                    }
                    $('view-all-link').show();
                    contentWin = null;
                    Windows.removeObserver(this);
                }
                //debug(eventName + " on " + win.getId())
            }
        }
        Windows.addObserver(myObserver);
    }
}

function search_filters(filter) {
	var url = "/_Find-Homes/query_txt.php";
	var getUrl = $("getUrl").value;
	var origUrl = getUrl;
	var allUrl = $('allListingsUrl').value;
	var allListings;
	getUrl += "&ajax=1";
	
	switch (filter) {
		case 'new_listings':
			if(origUrl.indexOf("&newlisting=yes") != -1) {
				//this means the user is already searching for newlistings
				allListings = true;
				break;
			}
			
			getUrl += '&newlisting=yes';
			origUrl += '&newlisting=yes';
			break;
		case 'open_houses':
			if(origUrl.indexOf("&ohouse=1") != -1) {
				//this means the user is already searching for open houses
				allListings = true;
				break;
			}
			
			getUrl += '&ohouse=1';
			origUrl += '&ohouse=1';
			break;
		case 'multiple_photos':
			if(origUrl.indexOf("&vtycount=2") != -1) {
				//this means the user is already searching for multiple photos
				allListings = true;
				break;
			}
			
			getUrl += '&vty=1&vtycount=2';
			origUrl += '&vty=1&vtycount=2';
			break;
		case 'virtual_tours':
			if(origUrl.indexOf("&vty=1") != -1) {
				//this means the user is already searching for virtual tours
				allListings = true;
				break;
			}
			
			getUrl += '&vty=1&vtycount=2';
			origUrl += '&vty=1&vtycount=2';
			break;
	}
	
	if (allListings == true) {
		$("all_listings_filter").style.color = "#006198";
		$("all_listings_filter").style.fontFamily = "helvetica,arial,sans-serif";
		$("all_listings_filter").style.fontSize = "14px";
		$("all_listings_filter").style.fontWeight = "bold";
		$("all_listings_filter").style.textDecoration = "underline";
		$("all_listings_filter").style.cursor = "pointer";
		Event.observe($("all_listings_filter"), 'click', function(){location.href=allUrl;});
		return true;
	}
	
	var myAjax = new Ajax.Request(url, {
        method: 'get',
        parameters: getUrl,
        asynchronous: false,
        onSuccess: function(transport) {   
            var response = transport.responseText;
            
            var responseObj = response.evalJSON(true);
            
        	if(responseObj.resultNum > 0) {
        		$(filter+"_filter").style.color = "#006198";
        		$(filter+"_filter").style.fontFamily = "helvetica,arial,sans-serif";
        		$(filter+"_filter").style.fontSize = "13px";
        		$(filter+"_filter").style.fontWeight = "bold";
        		$(filter+"_filter").style.textDecoration = "underline";
        		$(filter+"_filter").style.cursor = "pointer";
        		Event.observe($(filter+"_filter"), 'click', function(){location.href=origUrl;});
        	}                          
        }
    });
}

function showPhoneNum(row, uid, lid, page) {
	Element.show('phone_number'+row);
	Element.hide('phone_num_link'+row);
	
	var url = "/includes/tracking.php?action=phone&uid="+uid+"&lid="+lid+"&page="+page;
	
	var myAjax = new Ajax.Request(url, {
        method: 'get',
        asynchronous: false,
        onSuccess: function(transport) {   
            //we aren't doing any error checking here because we don't
			//really care if it fails, if it does fail we don't want to 
			//notify the user
        }
    });
}
