/*******************************************************************************

	CSS on Sails Framework
	Title: US Tracers
	Author: XHTMLized (http://www.xhtmlized.com/)
	Date: May 2011

*******************************************************************************/
function getServerTime(timeDelta) {
    var weekday = new Array(7);
    weekday[0] = "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Tuesday";
    weekday[3] = "Wednesday";
    weekday[4] = "Thursday";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";

    var serverTime = new Date((new Date()).getTime() + timeDelta);
    var dayOfWeek = serverTime.getDay();
    var dateDisplay = (serverTime.getMonth() + 1).toString() + '/' + serverTime.getDate() + '/' + serverTime.getFullYear() + ' ';
    var secDisplay = serverTime.getSeconds() < 10 ? '0' + serverTime.getSeconds() : serverTime.getSeconds().toString();
    var minDisplay = serverTime.getMinutes() < 10 ? '0' + serverTime.getMinutes() : serverTime.getMinutes().toString();
    var hourDisplay;
    if (serverTime.getHours() < 10) {
        hourDisplay = '0' + serverTime.getHours().toString();
    } else if (serverTime.getHours() <= 12) {
        hourDisplay = serverTime.getHours().toString();
    } else {
        hourDisplay = (serverTime.getHours() - 12).toString();
    }

    return weekday[dayOfWeek] + ", " + dateDisplay + hourDisplay + ':' + minDisplay + ':' + secDisplay + ' PM';
}

$(window).resize(function () {
    var loadingDiv = $('#loadingDiv');
    if (loadingDiv.length != 0) {
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(document).width();
        loadingDiv.css({ width: maskWidth, height: maskHeight });

        var loadingIcon = $('#loadingIcon');
        var win = $(window);
        var iconTop = win.scrollTop() + (win.height() - loadingIcon.height()) / 2;
        var iconLeft = win.scrollLeft() + (win.width() - loadingIcon.width()) / 2;
        loadingIcon.offset({ top: iconTop, left: iconLeft });
    }
});

$(window).unload(function () {
    var loadingDiv = $('#loadingDiv');
    if (loadingDiv.length != 0) {
        var loadingIcon = $('#loadingIcon');
        var win = $(window);
        var iconTop = win.scrollTop() + (win.height() - loadingIcon.height()) / 2;
        var iconLeft = win.scrollLeft() + (win.width() - loadingIcon.width()) / 2;
        loadingIcon.offset({ top: iconTop, left: iconLeft });
        loadingDiv.show();
    } 
});

$(document).ready(function () {
    var loadingDiv = $('#loadingDiv');
    if (loadingDiv.length != 0) {
        $('#loadingDiv').ajaxStart(function () {
            $(this).show();

            //Get the screen height and width
            var maskHeight = $(document).height();
            var maskWidth = $(document).width();
            $(this).css({ width: maskWidth, height: maskHeight });

            var loadingIcon = $('#loadingIcon');
            var win = $(window);
            var iconTop = win.scrollTop() + (win.height() - loadingIcon.height()) / 2;
            var iconLeft = win.scrollLeft() + (win.width() - loadingIcon.width()) / 2;
            loadingIcon.offset({ top: iconTop, left: iconLeft });
        });
        $('#loadingDiv').ajaxStop(function () {
            $(this).hide();
        });
    }

    //make HTML5 placeholders work in non supportive browsers
    $("input[placeholder]").each(function () {
        if ($(this).val() == "") {
            // $(this).addClass('hasplaceholder');
            $(this).val($(this).attr("placeholder"));
            $(this).focus(function () {
                if ($(this).val() == $(this).attr("placeholder")) $(this).val("");
                // $(this).removeClass('hasplaceholder');
            });
            $(this).blur(function () {
                if ($(this).val() == "") {
                    // $(this).addClass('hasplaceholder');
                    $(this).val($(this).attr("placeholder"));
                }
            });
        }
    });

    $('form').submit(function (evt) {
        $('input[placeholder]').each(function () {
            if ($(this).attr("placeholder") == $(this).val()) { $(this).val(''); }
        });
    });

    $(".clear-form a").live('click', function () {
        var form = $(this).parents('form:first');
        if (form.length != 0) {
            form.find('input:text').each(function () {
                $(this).val('');
            });
            form.valid();
        }
        return false;
    });

    $("a.print").live('click', function () {
        window.print();
        return false;
    });

    $("a.submitLinkBtn").live('click', function () {
        var form = $(this).parents('form:first');
        if (form.valid()) {
            form.submit();
        }
        return false;
    });

    $("a.submitLinkBtn").live('keydown', function () {
        var form = $(this).parents('form:first');
        form.submit();
        return false;
    });

    $('#login-form').keypress(function (e) {
        if (e.which == 13) {
            $(this).parents('form:first').submit();
            return false;
        }
    });

    //
    if ($('body').hasClass('home')) {
        Cufon.replace('#content .testimonial-wrapper h4, #content .social-strip div h4, #content .tabs ul.tabs-nav li a', { fontFamily: 'NeoSans-Bold' });
    }

    //
    $('.navigation li').has('ul').addClass('sub');

    //
    $("#content .tabs").tabs();

    //
    $('#content .results-wrapper thead tr th ul li.collapse a').each(function () {
        if ($(this).hasClass('switch-off')) {
            $(this).parents('.results-wrapper').children('tbody.content, tbody.edit, tbody.content-pagination').addClass('hide');
        }
    });

    //
    $('#content .results-wrapper thead tr th ul li.collapse a').live('click', function () {
        $(this).parents('.results-wrapper').children('tbody.content, tbody.content-pagination, .edit-wrapper-data').toggleClass('hide');
        $(this).parents('.results-wrapper').children('.edit-wrapper-form').children('tbody.edit').toggleClass('hide');
        $(this).toggleClass('switch-off');
        $(this).text($(this).text() == 'Expand' ? 'Collapse' : 'Expand');
        return false;
    });

    //
    $("#content .accordion .accordion-header .switch").click(function () {
        if ($(this).parent().parent().hasClass('accordion-off')) {
            $(this).parent().parent().removeClass('accordion-off');
            $(this).removeClass('switch-off');
        } else {
            $(this).parent().parent().addClass('accordion-off');
            $(this).addClass('switch-off');
        }
        return false;
    });

    //
    $("#content .accordion .accordion-header").hover(
		function () {
		    $(this).addClass("hover");
		},
		function () {
		    $(this).removeClass("hover");
		}
	);

    //
    $("#content .multiple-records table tbody tr td input").each(function () {
        if ($(this).is(':checked')) {
            $(this).parent().parent().addClass('checked');
        }
    });

    $("input[name='detailsID']:radio, input[name='resultSortingOption']:radio, input[name='batchDisplayOption']:radio, input[name='groupDisplayOption']:radio, input[name='searchOption']:radio, input[name='page']:radio").live('change', function () {
        var form = $(this).parents('form:first');
        form.submit();
        return false;
    });

    //
    if (($.browser.webkit)) {
        $('html').addClass('webkit');
    }

    //
    if (($.browser.mozilla)) {
        $('html').addClass('firefox');
    }

    //
    if (($.browser.msie) && ($.browser.version == "8.0")) {
        $("input[name='detailsID']:radio, input[name='SelectedFilterOption']:radio, input[name='resultSortingOption']:radio, input[name='batchDisplayOption']:radio, input[name='groupDisplayOption']:radio, input[name='searchOption']:radio, input[name='page']:radio").live('click', function () {
            this.blur();
            this.focus();
        });
    }

    //
    if (($.browser.msie) && ($.browser.version == "7.0")) {
        $("input[name='detailsID']:radio, input[name='SelectedFilterOption']:radio, input[name='resultSortingOption']:radio, input[name='batchDisplayOption']:radio, input[name='groupDisplayOption']:radio, input[name='searchOption']:radio, input[name='page']:radio").live('click', function () {
            this.blur();
            this.focus();
        });
    }

    //
    if (($.browser.msie) && ($.browser.version == "6.0")) {

        $('.ie6 form').css({ 'max-width': '1048px' });

        $("input[name='detailsID']:radio, input[name='SelectedFilterOption']:radio, input[name='resultSortingOption']:radio, input[name='batchDisplayOption']:radio, input[name='groupDisplayOption']:radio, input[name='searchOption']:radio, input[name='page']:radio").live('click', function () {
            this.blur();
            this.focus();
        });

    } else {

        //
        $('#content .carousel-wrapper .carousel').each(function () {
            $(this).slidedeck();
        });
        $('#content .nextBtn').each(function () {
            $(this).click(function () {
                $(this).parent().children('.carousel').slidedeck().next();
                return false;
            });
        });
        $('#content .prevBtn').each(function () {
            $(this).click(function () {
                $(this).parent().children('.carousel').slidedeck().prev();
                return false;
            });
        });

        //
        $('select').selectmenu();

    }
});


$(window).load(function() {

	if (($.browser.msie) && ($.browser.version == "7.0") ){

		// CSS3PIE
		var CSS3PIE_selectors = [
			'#content .user-wrapper ul li ul',
			'#content .user-wrapper ul li span',
			'#content .user-wrapper ul li',
			'#content .user-wrapper',
			'#content .edit-wrapper .edit .row-top .column textarea',
			'#content .edit-wrapper .edit .buttons li',
			'#content .edit-wrapper .edit .row .field  input',
			'#content .search-form .buttons li',
			'#content .search-form .buttons li a',
			'#content .search-form .middle .row .field  input',
			'#content .search-form .misc .reference input',
			'#content .search-form .misc',
			'#content .pagination2-wrapper dl dd ul li',
			'#content .results-wrapper thead tr th ul li',
			'.ui-state-active',
			'.ui-selectmenu',
			'#content .search-wrapper .text input',
			'#content .tools .buttons li',
			'#content .tools .pagination-wrapper p span',
			'#content .tools .pagination-wrapper p a',
			'#content .tools .pagination-wrapper p input',
			'#content .tools .pagination li a',
			'#content .batch-wrapper ul li ul',
			'#content .batch-wrapper ul li a.root',
			'#content .batch-wrapper ul',
			'.navigation li a span.count',
			'#content .tabs .tabs-content',
			'#content .tabs ul.tabs-nav li a',
			'#content .testimonial-wrapper .testimonial blockquote',
			'#content .testimonial-wrapper .testimonial',
			'#content .social-strip div input',
			'#content .social-strip',
			'#header .top li.search fieldset input',
			'#header .top li',
            '.popup_block',
            '.popupMenu',
            '#content div.contactUs',
            '#content div.contactUs div.button-submit',
            '#content div.leftCol div.header',
            '#content div.leftCol div.contents'
		];
		$( CSS3PIE_selectors.join(',') ).each(function() {
			PIE.attach(this);
		});
	}

	if (($.browser.msie) && ($.browser.version > "7.0") ){

		// CSS3PIE
		var CSS3PIE_selectors = [
			'#content .user-wrapper ul li ul',
			'#content .user-wrapper ul li span',
			'#content .user-wrapper ul li',
			'#content .user-wrapper',
			'#content .edit-wrapper .edit .row-top .column textarea',
			'#content .edit-wrapper .edit .buttons li',
			'#content .edit-wrapper .edit .row .field  input',
			'#content .search-form .buttons li',
			'#content .search-form .buttons li a',
			'#content .search-form .middle .row .field  input',
			'#content .search-form .misc .reference input',
			'#content .search-form .misc',
			'#content .pagination2-wrapper dl dd ul li',
			'#content .results-wrapper thead tr th ul li',
			'.ui-state-active',
			'.ui-selectmenu',
			'.ui-selectmenu-menu',
			'#content .search-wrapper .text input',
			'#content .tools .buttons li',
			'#content .tools .pagination-wrapper p span',
			'#content .tools .pagination-wrapper p a',
			'#content .tools .pagination-wrapper p input',
			'#content .tools .pagination li a',
			'#content .batch-wrapper ul li ul',
			'#content .batch-wrapper ul li a.root',
			'#content .batch-wrapper ul',
			'.navigation li a span.count',
			'#content .tabs .tabs-content',
			'#content .tabs ul.tabs-nav li a',
			'#content .testimonial-wrapper .testimonial blockquote',
			'#content .testimonial-wrapper .testimonial',
			'#content .social-strip div input',
			'#content .social-strip',
			'#header .top li.search fieldset input',
			'#header .top li',
            '.popup_block',
            '.popup_inner_block',
            '.popupMenu',
            '#content div.contactUs',
            '#content div.contactUs div.button-submit',
            '#content div.leftCol div.header',
            '#content div.leftCol div.contents'
		];
		$( CSS3PIE_selectors.join(',') ).each(function() {
			PIE.attach(this);
		});
	}
});

function ajaxOnWindowLoad(sectionId) {
    if (($.browser.msie) && ($.browser.version == "7.0")) {
        // CSS3PIE
        var CSS3PIE_selectors = [
			                '#' + sectionId + ' .edit-wrapper .edit .row-top .column textarea',
			                '#' + sectionId + ' .edit-wrapper .edit .buttons li',
			                '#' + sectionId + ' .edit-wrapper .edit .row .field  input',
			                '#' + sectionId + ' .pagination2-wrapper dl dd ul li',
			                '#' + sectionId + ' .results-wrapper thead tr th ul li',
			                '#' + sectionId + ' .ui-state-active',
			                '#' + sectionId + ' .ui-selectmenu'
		                    ];
        $(CSS3PIE_selectors.join(',')).each(function () {
            PIE.attach(this);
        });
    }

    if (($.browser.msie) && ($.browser.version > "7.0")) {
        // CSS3PIE
        var CSS3PIE_selectors = [
			            '#' + sectionId + ' .edit-wrapper .edit .row-top .column textarea',
			            '#' + sectionId + ' .edit-wrapper .edit .buttons li',
			            '#' + sectionId + ' .edit-wrapper .edit .row .field  input',
			            '#' + sectionId + ' .pagination2-wrapper dl dd ul li',
			            '#' + sectionId + ' .results-wrapper thead tr th ul li',
			            '#' + sectionId + ' .ui-state-active',
			            '#' + sectionId + ' .ui-selectmenu',
			            '#' + sectionId + ' .ui-selectmenu-menu'
		            ];
        $(CSS3PIE_selectors.join(',')).each(function () {
            PIE.attach(this);
        });
    }
}
