﻿$(document).ready(function () {

    // search field gray text before active
    $("input.searchBoxGray").css('color', 'gray');
    $("input.searchBoxGray").one('click', function () {
        $(this).css('color', '#000');
        $(this).val("");
    });
    $("input.searchBoxGray").one('keydown', function () {
        $(this).css('color', '#000');
        $(this).val("");
        $(this).unbind('click');
    });

    // Add pdf icons to pdf links
    $(".attachments a").addClass("download"); //default icon, might be overridden later    
    $(".attachments a[href$='.pdf']").addClass("pdf");
    $(".attachments a[href$='.mp3'], .attachments a[href$='.wav']").addClass("audio");
    $(".attachments a[href$='.doc'], .attachments a[href$='.rtf'], .attachments a[href$='.docx']").addClass("word");
    $(".attachments a[href$='.xls'], .attachments a[href$='.xlsx'], .attachments a[href$='.csv']").addClass("excel");
    $(".attachments a[href$='.jpg'], .attachments a[href$='.png'], .attachments a[href$='.gif'], .attachments a[href$='.tif'], .attachments a[href$='.tiff'], .attachments a[href$='.bmp']").addClass("picture");
    $(".attachments a[href$='.avi'], .attachments a[href$='.flv'], .attachments a[href$='.wmv'], .attachments a[href$='.mov'], .attachments a[href$='.divx'], .attachments a[href$='.mpeg']").addClass("video");
    $(".attachments a[href$='.zip'], .attachments a[href$='.rar']").addClass("zip");

    if ($(".cmsContent0, .cmsContent1, .cmsContent2, .cmsContent3").imagePopup)
        $(".cmsContent0, .cmsContent1, .cmsContent2, .cmsContent3").imagePopup();

    if ($("#faq").makeFAQ) {
        $("#faq,#faq2,#faq3,#faq4,#faq5").makeFAQ({ indexTitle: 'Frequently asked questions', displayIndex: false, faqHeader: 'h4' });
    }

    if ($("#frontPageTopArea").size() > 0) {
        $("#frontPageTopArea .dropper h3").click(function () {
            $("#frontPageTopArea .dropper .element.selected .iwContent").slideUp();
            $("#frontPageTopArea .dropper .element").removeClass("selected");
            $(this).parent().addClass("selected");
            $("#frontPageTopArea .dropper .element.selected .iwContent").slideDown();
        });
    }

    /*
    // Add email icons to email links
    $("a[href^='mailto:']").addClass("email");

    //Add external link icon to external links - 
    $('a').filter(function() {
    //Compare the anchor tag's host name with location's host name
    return this.hostname && this.hostname !== location.hostname;
    }).addClass("external");
    */

    //You might also want to set the _target attribute to blank
    /*
    $('a').filter(function() {
    //Compare the anchor tag's host name with location's host name
    return this.hostname && this.hostname !== location.hostname;
    }).addClass("external").attr("target", "_blank");
    */
});

(function ($) {
    $.fn.imagePopup = function () {
        $(this).find("a img").parent().each(function(n, val){
            $(val).addClass("imagePopup");
            $(val).attr("rel", n);
        });
        
        /*$("#tinybox a#next").live('click', function(){
            var rel = parseInt($(this).parent().find("a").attr("rel"));
            $("a.imagePopup[rel=" + (rel+1) + "]").click();
        });
        $("#tinybox a#prev").live('click', function(){
            var rel = parseInt($(this).parent().find("a").attr("rel"));
            $("a.imagePopup[rel=" + (rel-1) + "]").click();
        });*/
        $("#tinybox a#close").live('click', function(){
            TINY.box.hide();
            return false;
        });

        $("a.imagePopup").click(function () {
            var href = $(this).attr('href');
            var alt = $(this).find("img").attr('alt');
            var rel = parseInt($(this).attr('rel'));

            if (href && href.indexOf('/upload') > -1 && (href.toLowerCase().indexOf('.jpg') > -1 || href.toLowerCase().indexOf('.jpeg') > -1 || href.toLowerCase().indexOf('.png') > -1)) {                
                $("body").append($("<img src=\"/App_Themes/CrosshairsEmbedded/images/ajax-loader.gif\" alt=\"\" />").attr("id", "loader").attr("style", "position: fixed; top: 50%; left: 50%; z-index: 1000"));
                $.imgpreload(href, function () {
                    $("#loader").remove();

                    // browser size:
                    var w = Math.round($(window).width() * 0.9);
                    var h = Math.round($(window).height() * 0.9);
                            
                    var next = "";
                    var prev = "";            
                    /*if($("a.imagePopup[rel=" + (rel+1) + "]").size()>0){
                        next = "<a href='#' id='next'>Next 	&#155;</a>";
                    }
                    if($("a.imagePopup[rel=" + (rel-1) + "]").size()>0){
                        prev = "<a href='#' id='prev'>&#139; Prev</a>";
                    }*/
                    TINY.box.show("<a href=\"#close\" id=\"close\" rel=\"" + rel + "\"><img src=\"" + href + "\" alt=\"" + alt + "\" style=\"max-width: " + w + "px; max-height: " + h + "px;\" /></a>" + prev + next, 0, 0, 0, 1);
                    
                });
                return false;
            }
        });
    };
})(jQuery);
