/**** GLOBAL VARIABLES ****/

// Carousel default vars, you can override these locally on that page's document ready function
var carouselStart = 1;
var carouselScroll = 1;


/**** DOCUMENT READY INITIALISTATIONS ****/

jQuery(document).ready(function () {
    // Wire up a click event for the popuplink
    jQuery('.openImage, .areaMapOverlay').click(function () {
        openLightbox(this);
        return false;
    });

    jQuery('.signInOverlay').click(function () {
        var lightbox = new iframeLightbox(this.href, 360, 620);
        lightbox.CreateLightbox();
        return false;
    });

    jQuery('.registerOverlay').click(function () {
        var lightbox = new iframeLightbox(this.href, 360, 606);
        lightbox.CreateLightbox();
        return false;
    });

    jQuery('.areaGuideOverlay').click(function () {
        var lightbox = new iframeLightbox(this.href, 706, 528);
        lightbox.CreateLightbox();
        return false;
    });

    $("fieldset.buyRent:even").removeClass("buyRentNoScript");

    // style radio buttons BUY RENT 
    $('fieldset.buyRent span').click(function (e) {
        $('fieldset.buyRent span.selected').removeClass('selected');
        $('fieldset.buyRent').children("input[@type=radio]").removeAttr('checked');
        $(this).children("input[@type=radio]").attr('checked', 'checked');
        $(this).addClass('selected');
        $(this).children("input[@type=radio]").click();
        return false; // prevents the event being called multiple times
    });

    $('fieldset.buyRent span').children('input[@type=radio]').each(function () {
        if ($(this).is(':checked')) {
            $(this).parent().addClass('selected');
        }
    });

    // add selectesd class to Lifestle search list box element
    $('.lifestyleThumbPanels label, .lifestyeRadio').click(function () {
        if ($(this).is(':checked')) {
            $('li.selected').removeClass('selected');
            $(this).parents("li").addClass('selected');
        }

        // commented out as eventully crashes browser
        /* fixes IE bug, allowing img to select radio
        $("label img").live("click", function() {
        $("#" + $(this).parents("label").attr("for")).click();
        });
        */

    });



    /* refurb tabs */

    // css added here for js version allows non-js to display in page
    $('.refurbContainer').css({ 'height': '300px' });
    $('.refurbContainer .refurbInner').css({ 'position': 'absolute', 'width': '1950px' });
    $('.refurbContainer .refurbProjects').css({ 'float': 'left' });
    $('.refurbContainer .subNavA').css({ 'display': 'block', 'padding-top': '250px' });

    // highlighting for current tab
    $('.refurbContainer .subNavA li a').click(function () {
        $('.refurbContainer .subNavA li').removeClass('currentTab');
        $(this).parents().addClass('currentTab');
    });

    // slide effect for projects
    $('.refurbContainer .tab1').click(function () {
        $('.refurbContainer .refurbInner').animate({ 'left': '0px' });
        return false;
    });
    $('.refurbContainer .tab2').click(function () {
        $('.refurbContainer .refurbInner').animate({ 'left': '-650px' });
        return false;
    });
    $('.refurbContainer .tab3').click(function () {
        $('.refurbContainer .refurbInner').animate({ 'left': '-1300px' });
        return false;
    });

    /* end refurb tabs */


    /* =============== */


    /* area guide tabs */

    // css added here for js version allows non-js to display in page
    $('.areaGuideLanding .tabContent').css({ 'display': 'none' });
    $('.areaGuideLanding .overview').css({ 'display': 'block' });

    // highlighting for current tab
    $('.areaGuideLanding .subNavA li a').click(function () {
        $('.areaGuideLanding .subNavA li').removeClass('currentTab');
        $(this).parents().addClass('currentTab');
    });

    // fade effect for tab switching

    $('.areaGuideLanding .tab1').click(function () {
        $('.areaGuideLanding .tabContent').css({ 'display': 'none' });
        $('.areaGuideLanding .overview').fadeIn('slow');
        return false;
    });
    $('.areaGuideLanding .tab2').click(function () {
        $('.areaGuideLanding .tabContent').css({ 'display': 'none' });
        $('.areaGuideLanding .shopping').fadeIn('slow');
        return false;
    });
    $('.areaGuideLanding .tab3').click(function () {
        $('.areaGuideLanding .tabContent').css({ 'display': 'none' });
        $('.areaGuideLanding .sports').fadeIn('slow');
        return false;
    });

    /* end area guide tabs */


    /* =================== */


    /* more about accounts tabs */

    // hide infoArea 2 - 4
    $('.moreAboutTabs a').removeAttr('href').css({ 'cursor': 'pointer' });
    $('.moreAboutInfoArea .infoAreaContent').css({ 'display': 'none' });
    $('.moreAboutInfoArea .infoArea1').css({ 'display': 'block' });

    // current tab function
    $('.moreAboutTabs a').click(function () {
        $('.moreAboutTabs li').removeClass('current');
        $(this).parents('li').addClass('current');
        return false;
    });

    // show / hide function
    $('.moreAboutTabs a').click(function () {
        $('.moreAboutInfoArea .infoAreaContent').css({ 'display': 'none' });
    });

    $('.moreAboutTabs .infoTab1 a').click(function () {
        $('.moreAboutInfoArea .infoArea1').css({ 'display': 'block' });
    });
    $('.moreAboutTabs .infoTab2 a').click(function () {
        $('.moreAboutInfoArea .infoArea2').css({ 'display': 'block' });
    });
    $('.moreAboutTabs .infoTab3 a').click(function () {
        $('.moreAboutInfoArea .infoArea3').css({ 'display': 'block' });
    });
    $('.moreAboutTabs .infoTab4 a').click(function () {
        $('.moreAboutInfoArea .infoArea4').css({ 'display': 'block' });
    });

    /* end more about accounts tabs */


    /* =================== */


    /* more about accounts tabs */

    // hide infoArea 2 - 4
    $('.noticesTabs a').removeAttr('href').css({ 'cursor': 'pointer' });
    $('.noticesInfoArea .noticesContent').css({ 'display': 'none' });
    $('.noticesInfoArea .notices1').css({ 'display': 'block' });

    // current tab function
    $('.noticesTabs a').click(function () {
        $('.noticesTabs li').removeClass('current');
        $(this).parents('li').addClass('current');
        return false;
    });

    // show / hide function
    $('.noticesTabs a').click(function () {
        $('.noticesInfoArea .noticesContent').css({ 'display': 'none' });
    });

    $('.noticesTabs .noticesTab1 a').click(function () {
        $('.noticesInfoArea .notices1').css({ 'display': 'block' });
    });
    $('.noticesTabs .noticesTab2 a').click(function () {
        $('.noticesInfoArea .notices2').css({ 'display': 'block' });
    });
    $('.noticesTabs .noticesTab3 a').click(function () {
        $('.noticesInfoArea .notices3').css({ 'display': 'block' });
    });
    $('.noticesTabs .noticesTab4 a').click(function () {
        $('.noticesInfoArea .notices4').css({ 'display': 'block' });
    });

    /* end more about accounts tabs */

    /*
    * IE7 A-Z Footer Fix (IE7 positions the A-Z footer incorrectly because it works out the absolute positioning before the entire page has loaded)
    */
    var ie7FooterFix = function () {
        if($.browser.msie && $.browser.version == '7.0') {
            $('.azLinks').css('bottom', '125px');
        }
    }();
    
});


/**** SHARED FUNCTIONS ****/

/* Initialises the carousel */
function carouselInit(carouselDiv) {
	// Adds formatting so JS and non-JS versions look correct...
	if(carouselDiv == '.carouselSale' || carouselDiv == '.carouselViewed' || carouselDiv == '.carouselFeatured') {
		$(carouselDiv + " li span:even").removeClass("descriptionNoScript");
		$(carouselDiv + " li span:even").addClass("description");
	}
	
    // Calls the carousel function to build carousel
	// Global variables are set at the top of this file, but can be overwritten locally on that page's document ready function
    jQuery(carouselDiv).jcarousel({
    	start  : carouselStart,
    	scroll : carouselScroll
    });
}


/*
* Disables buttons on forms while waiting for user input
*/
function btnDisable(theButtonID, targetCheckbox) {
    $(targetCheckbox).attr('checked', '');
    $(theButtonID).attr('disabled', 'disabled'); // Disables the button
    $(theButtonID).addClass('btnDisabled');
    //$(theButtonID).css({'opacity' : '0.4', 'cursor' : 'default'}); // Adds CSS so it looks disabled
}

/*
* Enables buttons on forms after user input
*/
function btnEnable(theButtonID, targetCheckbox) {
    $(targetCheckbox).attr('checked', 'checked');
    $(theButtonID).attr('disabled', ''); // Enables the button
    $(theButtonID).removeClass('btnDisabled');
    //$(theButtonID).css({'opacity' : '1', 'cursor' : 'pointer'}); // Adds CSS so it looks enabled
}



function zoomIn(elem) {
    var el = document.getElementById(elem);

    elWidth = el.clientWidth;
    elHeight = el.clientHeight;

    var distanceInLeft = ((648 / 2) - el.offsetLeft) / el.width;
    var distanceInTop = ((526 / 2) - el.offsetTop) / el.height;
    
    //el.style.width = elWidth * 1.2 + 'px';
    //el.style.height = elHeight * 1.2 + 'px';
    zoomUpdateImage(el, elWidth * 1.2, elHeight * 1.2);
    
    el.style.left = ((648 / 2) - (distanceInLeft * el.width)) + "px";
    el.style.top = ((526 / 2) - (distanceInTop * el.height)) + "px";

    var el = document.getElementById(elem);
    var leftEdge = el.parentNode.clientWidth - el.clientWidth;
    var topEdge = el.parentNode.clientHeight - el.clientHeight;
    var dragObj = new dragObject(el, null, new Position(leftEdge, topEdge), new Position(0, 0));
}

function zoomUpdateImage(el, width, height) {
    width = Math.round(width);
    height = Math.round(height);
    
    // http://localhost:4809/property/38359/floorplans/0/0/floorplan_38359_26.gif
    el.src = el.src.replace(new RegExp("\/[0-9]+\/[0-9]+\/", "m"), "/" + width + "/" + height + "/");
    if (width > 0) {
        el.style.width = width + 'px';
    }

    if (height > 0) {
        el.style.height = height + 'px';
    }
}

function zoomOut(elem) {
    var el = document.getElementById(elem);

    elWidth = el.clientWidth;
    elHeight = el.clientHeight;

    var parentWidth = el.parentNode.clientWidth;
    var parentHeight = el.parentNode.clientHeight;

    var distanceInLeft = ((648 / 2) - el.offsetLeft) / el.width;
    var distanceInTop = ((526 / 2) - el.offsetTop) / el.height;

    //el.style.width = elWidth / 1.2 + 'px';
    //el.style.height = elHeight / 1.2 + 'px';
    zoomUpdateImage(el, (elWidth / 1.2), (elHeight / 1.2));
    
    el.style.left = ((648 / 2) - (distanceInLeft * el.width)) + "px";
    el.style.top = ((526 / 2) - (distanceInTop * el.height)) + "px";
}

/* my account edit details */
jQuery(document).ready(function() {
	$('.changePasswordFields').css({'display': 'none'});
	$('.changePasswordLink').click(function() {
		$('.changePasswordFields').toggle('fast');
		return false;
	});
	return false;
});	

/*
* Provides Hover states dng map
*/
function MapHoverEnable(location) {

    if (location != null) {
  	    $("input[name='SearchCriteria.Location']").val('');

        var areas = location.split(',');
        var selector;

	    for (i = 0; i < areas.length; i++) {
            selector = '.areaMapHolder area[title="' + $.trim(areas[i]) + '"]';

            if ($(selector).length > 0) {
                SelectMapArea($(selector));
            }
	    }
    }

    // hover states
    $('.areaMapHolder area').hover(function () {
        $('.areaMapHolder .selected').addClass($(this).attr('class') + 'Selected');
    },
	function () {
	    $('.areaMapHolder .selected').removeClass($(this).attr('class') + 'Selected');
	});

    // click function
	$('#searchAreaMap area').click(function () {
	    var locationInput = $("input[name='SearchCriteria.Location']");

	    if (locationInput.hasClass('watermark')) {
	        locationInput.val('').removeClass('watermark');
	    }

        SelectMapArea($(this));
	});
}

function SelectMapArea(area) {
	var name = area.attr('class');
	var title = area.attr('title');
	var id = name + 'SelectedOverlay';

	var locationInput = $("input[name='SearchCriteria.Location']");

    if ($('#' + id).length > 0) {
        $('#' + id).remove();

        var areas = locationInput.val().split(',');
        var value = '';
        
        for (i = 0; i < areas.length; i++) {
            if (areas[i] != title) {
                value = value + ',' + areas[i];
            }
        }

        value = value.substr(1, value.length - 1);

        locationInput.val(value);
    }
    else {
        $('.areaMapHolder').append('<div class="' + name + 'Selected" id="' + id + '"></div>');

        if (locationInput.val().length > 0) {
            locationInput.val(locationInput.val() + "," + title);
        }
        else {
            locationInput.val(title);
        }
    }

}


/*
 *  Graph carousel JS
 *  This allows us to use our own HTML and code for previous and next in the carousel so we can add text in between..
 */

var graphCarousel = function (carousel) {
    jQuery('.graphControls a').bind('click', function () {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });

    jQuery('.graphControls select').bind('change', function () {
        carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
        return false;
    });

    jQuery('.graphNext').bind('click', function () {
        carousel.next();
        return false;
    });

    jQuery('.graphPrev').bind('click', function () {
        carousel.prev();
        return false;
    });
}
