﻿
jQuery(document).ready(function () {
    reveal();
    hover_fade();
    nav();
    nav_icon();
});

function reveal() {
    $(".cl_reveal, .cl_top p").click(function () {
        $(".cl_mid, .cl_mid2").slideToggle("normal");
        $(this).toggleClass("active");
        return false;
    });

    $(".link_header").click(function () {
        $(this).next().slideToggle("normal");
        $(this).toggleClass("active");
        return false;
    });
}

function hover_fade() {
    $(".psr_prop_img, .cs_image_holder").show();
    $(".psr_prop_img img, .cs_image_holder img").fadeTo('normal', 1, function () {

        $(this).hover(function () {
            $(this).fadeTo('normal', 0.5);

        }, function () {
            $(this).fadeTo('normal', 1);
        });

    });
}

// Nav START

function nav_icon() {
    $("ul.subnav").parent().children("a").before('<img class="nav_img" src="assets/images/nav_dd_img.png" alt="Nav Img" />');
    $("ul.subnav").parent().children("a").addClass("dd");

    $('.subnav li:nth-child(1)').addClass('first_item');

    $('.subnav li:last-child').addClass('last_item');
    $('.subnav li:last-child').children().addClass('last_item');
}

function mouseOver() { $(this).find("ul.subnav").slideDown('standard').show(); }
function mouseOut() { $(this).find("ul.subnav").slideUp('standard'); }

function nav() {
    var config = {
        over: mouseOver, // function = onMouseOver callback (REQUIRED)    
        timeout: 500, // number = milliseconds delay before onMouseOut    
        out: mouseOut // function = onMouseOut callback (REQUIRED)    
    };

    $('ul.topnav li').hoverIntent(config);    
}
// Nav END

// Clear Placeholder START
function clearIt(what) {
    if (what.value == what.defaultValue) what.value = '';
}
function setIt(what) {
    if (what.value == '') what.value = what.defaultValue;
}
// Clear Placeholder END

// Scrolltop Control START
$(function () {
    sh = $(window).height() / 2;
    xh = $(document).height() / 2 - sh;
});

var scrolltotop = {

    setting: { startline: 200, scrollto: 0, scrollduration: 1000, fadeduration: [200, 50] },
    controlHTML: '<span class="scroll_up"></span>',
    controlattrs: { offsetx: 5, offsety: 5 },
    anchorkeyword: '#top',
    state: { isvisible: false, shouldvisible: false },

    scrollup: function () {
        if (!this.cssfixedsupport)
            this.$control.css({ opacity: 0 })
        var dest = isNaN(this.setting.scrollto) ? this.setting.scrollto : parseInt(this.setting.scrollto)
        if (typeof dest == "string" && jQuery('#' + dest).length == 1)
            dest = jQuery('#' + dest).offset().top
        else
            dest = 0
        this.$body.animate({ scrollTop: dest }, this.setting.scrollduration);
    },

    keepfixed: function () {
        var $window = jQuery(window)
        var controlx = $window.scrollLeft() + $window.width() - this.$control.width() - this.controlattrs.offsetx
        var controly = $window.scrollTop() + $window.height() - this.$control.height() - this.controlattrs.offsety
        this.$control.css({ left: controlx + 'px', top: controly + 'px' })


    },

    togglecontrol: function () {
        var scrolltop = jQuery(window).scrollTop()
        if (!this.cssfixedsupport)
            this.keepfixed()
        this.state.shouldvisible = (scrolltop >= xh) ? true : false
        if (this.state.shouldvisible && !this.state.isvisible) {
            this.$control.stop().animate({ opacity: 1 }, this.setting.fadeduration[0])
            this.state.isvisible = true
        }
        else if (this.state.shouldvisible == false && this.state.isvisible) {
            this.$control.stop().animate({ opacity: 0 }, this.setting.fadeduration[2])
            this.state.isvisible = false
        }
    },

    init: function () {
        jQuery(document).ready(function ($) {
            var mainobj = scrolltotop
            var iebrws = document.all
            mainobj.cssfixedsupport = !iebrws || iebrws && document.compatMode == "CSS1Compat" && window.XMLHttpRequest //not IE or IE7+ browsers in standards mode
            mainobj.$body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body')
            mainobj.$control = $('<div id="topcontrol"><span class="scroll_up"></span></div>')
				.css({ position: mainobj.cssfixedsupport ? 'fixed' : 'absolute', bottom: mainobj.controlattrs.offsety, right: mainobj.controlattrs.offsetx, opacity: 0, cursor: 'pointer' })
				.attr({ title: 'Scroll Back to Top' })
				.click(function () { mainobj.scrollup(); return false })
				.appendTo('body')
            if (document.all && !window.XMLHttpRequest && mainobj.$control.text() != '') //loose check for IE6 and below, plus whether control contains any text
                mainobj.$control.css({ width: mainobj.$control.width() }) //IE6- seems to require an explicit width on a DIV containing text
            mainobj.togglecontrol()
            $('a[href="' + mainobj.anchorkeyword + '"]').click(function () {
                mainobj.scrollup()
                return false
            })
            $(window).bind('scroll resize', function (e) {
                mainobj.togglecontrol()
            })
        })
    }
}

if($("#top").length>0)
    scrolltotop.init()
// Scrolltop Control END

// Popup Window

function popup(url) {
    var width = 800;
    var height = 600;
    var left = (screen.width - width) / 2;
    var top = (screen.height - height) / 2;
    var params = 'width=' + width + ', height=' + height;
    params += ', top=' + top + ', left=' + left;
    params += ', directories=no';
    params += ', location=no';
    params += ', menubar=no';
    params += ', resizable=no';
    params += ', scrollbars=1';
    params += ', status=no';
    params += ', toolbar=no';
    newwin = window.open(url, 'windowname5', params);
    if (window.focus) { newwin.focus() }
    return false;
}

function populateXMLDropDown(xmlLocation, field, dataName, IdName, selection) {

    $.ajax({
        type: "GET",
        url: xmlLocation,
        dataType: "xml",
        success: function (xml) {
            var select = $(field);
            select.find('option:not(:first)').remove();
            $(xml).find('d').each(function () {
                var title = $(this).attr(dataName);
                var value = $(this).attr(IdName);
                if (selection == value) {
                    select.append($('<option>' + title + '</option>').val(value).attr('selected', 'selected'));
                } else {
                    select.append($('<option>' + title + '</option>').val(value));
                }

            });

        }
    });
}
