var site = "http://www.operationlandlord.com";
var colHeight = 0;

// custom function to reverse an array in jQuery
$.fn.reverse = [].reverse;

// Stuff to fire after all pages images have loaded
$(window).load(function() {
    // Equalize column heights

    $("div.column-inner").each(
        function(i) {
            if ($(this).height() > colHeight) {
                colHeight = $(this).height();
            }
        }
    ).height(colHeight);
});

// stuff to fire when the DOM is ready
$(document).ready(function() {

    // Fix for getting URL before re-write
    if ($("#fromHref").length > 0) {
        $("#fromHref").val(document.location);
    }

    // Set min height on #content area
    if ($("#content").height() < 450) {
        $("#content").height(450);
    }

    // Button-style links and form buttons
    SimplyButtons.init();

    // -------------------------------------------
    // USER & PRESS ADS - carousel
    if ($("#advert-carousel").length > 0) {
        $("#btn-carousel-next, #btn-carousel-prev").click(function() { return false; });
    }

    if ($("#advert-carousel").length > 0 && $("#advert-carousel ul li").length > 4) {
        $("#advert-carousel").jCarouselLite({
            btnNext: "#btn-carousel-next",
            btnPrev: "#btn-carousel-prev",
            visible: 4,
            scroll: 4,
            speed: 1500,
            beforeStart: function(el) { onBeforeCycleAds(el); }
        });
    } // end.


/*
    $("li.press-ad a.press-frame-link").click(function() {
        pageTracker._trackEvent('POS Poster', 'Viewed', $(this).attr("title"));
    });
*/

    // USER & PRESS ADS - ad switch
    $(".press-ad a").click(function() {
        var sSrc = $(this).attr("href");
        var guid = $(this).attr("id");
        var title = $(this).attr("title");
        var data = $(this).attr("rel");
        var shareUrl = "";
        var id = "";
        
        if (data.indexOf("||") != -1) {
            var arr = data.split("||");
            id = arr[0];
        } else {
            id = data;
        }

        $("#advert-main").css("background-image", "url(" + sSrc + ")");
        $("#advert-main img").attr({ id: guid, alt: id });


        return false;
    }); // end.

    // PRESS ADS - carousel button states
    $("#advert-carousel-prev a")
        .bind("mouseout", function() { $("img", this).css("background-position", "0 0"); })
        .bind("mousedown", function() { $("img", this).css("background-position", "0 -104px"); })
        .bind("mouseover", function() { $("img", this).css("background-position", "0 -52px"); })
        .bind("mouseup", function() { $("img", this).css("background-position", "0 -52px"); });

    $("#advert-carousel-next a")
        .bind("mouseout", function() { $("img", this).css("background-position", "-52px 0"); })
        .bind("mousedown", function() { $("img", this).css("background-position", "-52px -104px"); })
        .bind("mouseover", function() { $("img", this).css("background-position", "-52px -52px"); })
        .bind("mouseup", function() { $("img", this).css("background-position", "-52px -52px"); });

    $("#advert-carousel-key a").click(function() { return false; });


    // -------------------------------------------
    // CONTACT FORM: submit button roll-over
    if ($("#ctl00_MainContent_frmContactSubmit").length > 0) {
        $("#ctl00_MainContent_frmContactSubmit").hover(
            function() { $(this).css("background-position", "-209px -224px"); },
            function() { $(this).css("background-position", "-2px -224px"); }
        );
    } // end.

    // -------------------------------------------
    // PASSWORD FORM: submit button roll-over
    if ($("#ctl00_MainContent_bttFPassword").length > 0) {
        $("#ctl00_MainContent_bttFPassword").hover(
            function() { $(this).css("background-position", "0 -203px"); },
            function() { $(this).css("background-position", "0 0"); }
        );
    } // end.    

    // -------------------------------------------
    // SIGNUP FORM: submit button roll-over
    if ($("#ctl00_MainContent_frmSignupSubmit").length > 0) {
        $("#ctl00_MainContent_frmSignupSubmit").hover(
            function() { $(this).css("background-position", "0 -203px"); },
            function() { $(this).css("background-position", "0 0"); }
        );
    } // end.


    // SIGNUP FORM: custom drop-downs
    $(".custom-dropdown").each(function() { createDropDown($(this)); });

    $(".dropdown dt a").click(function() {
        $(".dropdown dd ul").hide();
        $("dd ul", $(this).parent().parent()).toggle();
        return false;
    });

    $(document).bind('click', function(e) {
        var $clicked = $(e.target);
        if (!$clicked.parents().hasClass("dropdown"))
            $(".dropdown dd ul").hide();
    });

    $(".dropdown dd ul li a").click(function() {
        var text = $(this).html();
        var sourceID = $(this).attr("rel");
        $("dt a", $(this).parents('.dropdown')).html(text);
        $(".dropdown dd ul").hide();

        var source = $("#" + sourceID);
        source.val($("#dd-" + sourceID).find("span.value").html());

        return false;
    });


    // -------------------------------------------
    // Find flash embed class links to replace with Fash SWF 
    $(".flash-embed").each(function() {

        var sID = $(this).attr("id");

        if (sID != "header-title")
            sID = $("a:first", this).attr("id");

        var sSWF = $("a:first", this).attr("rel");
        var iWidth = $(this).width();
        var iHeight = $(this).height();

        swfobject.embedSWF(
            sSWF,
            sID,
            iWidth,
            iHeight,
            "9.0.0",
            "/swf/expressInstall.swf",
            {},
            { wmode: "opaque" },
            { id: sID, name: sID }
        );

    }); //end.


    // -------------------------------------------
    // link buttons
    $(".link-panel-button")
        .bind("mouseout", function() { $("img", this).css("background-position", "0 0"); })
        .bind("mouseover", function() { $("img", this).css("background-position", "0 -73px"); })
        .bind("mousedown", function() { $("img", this).css("background-position", "0 -146px"); })
        .bind("mouseup", function() { $("img", this).css("background-position", "0 -73px"); });



});

// -------------------------------------------------------------------------------------------------------------------------------------

// Just-in-time loading of carousel images. 
// Fires before the transition on Cycle Lite when prev / next button is clicked.
function onBeforeCycleHome(el) {

    if ($('a:first img', el).length == 0) {
        var sSrc = $('a:first', el).attr('rel');
        var sAlt = $('a:first', el).attr('title');

        $.ajax({
            url: sSrc,
            async: false,
            cache: true,
            beforeSend: function() { $("#home-carousel-loader").show(); },
            complete: function() { $("#home-carousel-loader").hide(); },
            error: function() { $("#home-carousel-loader").hide(); return false; }
        });
        
        oImage = new Image();
        oImage.src = sSrc;        
        $('a:first', el).html('<img src="' + sSrc + '" alt="' + sAlt + '"/>');
        return true;
    }

}

// Just-in-time loading of carousel thumbnails. 
// Fires before the transition on jCarousel Lite when prev / next button is clicked.
function onBeforeCycleAds(el) {

    $(el).each(function() {
        var bg = $(this).css("background-image");

        if (bg == 'none' || bg == '') {
            
            var sSrc = $(this).attr("title");
            
            oImage = new Image();
            oImage.src = sSrc;

            bg = 'url(' + sSrc + ')';
            $(this).removeAttr("title").css("background-image", bg);
        }
    });

    // get page index and update page key
    var pages = $('#advert-carousel-key li').size();
    var i = ($("li.press-ad, li.tv-ad").index($(el)) / 4);
    if (i > pages) {
        i = 0;
    } else {
        i = i - 1;
    }
    $('#advert-carousel-key li').removeClass('on');
    $('#advert-carousel-key li:eq(' + i + ')').addClass('on');
    
    
} //ends.

// Generates a <DL><DT><DD></DL> html section to replace 
// form elements <SELECT><OPTION/></SELECT> for custom designed drop-downs.
function createDropDown(source) {

    var sourceID = source.attr("id");
    var selected = source.find("option[selected]");
    var options = $("option", source);
    var iWidth = source.css("width");
    var iHeight = source.css("height");

    source.parent().append('<dl id="dd-' + sourceID + '" class="dropdown"></dl>');

    $("#dd-" + sourceID)
        .append('<dt><a href="#">' + selected.text() + '<span class="value">' + selected.val() + '</span></a></dt>')
        .append('<dd><ul></ul></dd>');

    options.each(function() {
        $("#dd-" + sourceID + " dd ul").append('<li><a href="#" rel="' + sourceID + '">' + $(this).text() + '<span class="value">' + $(this).val() + '</span></a></li>');        
    });

    source.hide();

    $("#dd-" + sourceID + " dt a").css({ width: iWidth, height: iHeight });
}





var userOSAgent = navigator.userAgent.toLowerCase();

// Figure out what browser is being used
jQuery.platform = {
    mac: /mac/.test(userOSAgent),
    osx: /mac os x/.test(userOSAgent),
    win: /win/.test(userOSAgent),
    linux: /linux/.test(userOSAgent)
};

