﻿$(document).ready(function () {

	// 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");


	/*
	// 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");
	*/

	// search field gray text before active
	$(".searchBoxGray").css('color', '#000');
	var grayF = function () {
		$(this).css('color', '#000');
		$(this).val("");
		$(this).unbind('click.zgray');
		$(this).unbind('keeydown.zgray');
	};
	$(".searchBoxGray").one('click.zgray', grayF);
	$(".searchBoxGray").one('keydown.zgray', grayF);

	if ($(".popup").imagePopup) {
		$(".popup").imagePopup();
	}

	/* egil */
	if (typeof(draftID) !== "undefined") {
		$('.pristabell').show();
	}

});

function fillTextBox(place) {
    switch(place){
        case 'name':
            $(".delName").val($(".newName").val());
            if ($(".invoName").length) {
                $(".invoName").val($(".newName").val());
            }
            break;
        case 'surname':
            $(".delSurname").val($(".newSurname").val());
            if ($(".invoSurname").length) {
                $(".invoSurname").val($(".newSurname").val());
            }
            break;
        default:
            break;
        }
}

(function ($) {
    $.fn.imagePopup = function () {
        var elems = $(this).find("img");
        elems.each(function (n, val) {
            var a = $(val).parent();
            $(a).click(function () {
                if (a.attr('href').indexOf('/upload') > -1) {
                    var url = a.attr('href');
                    $.imgpreload(url, function () {

                        // browser size:
                        var w = Math.round($(window).width() * 0.9);
                        var h = Math.round($(window).height() * 0.9);

                        TINY.box.show("<img src=\"" + url + "\" alt=\"\" style=\"max-width: " + w + "px; max-height: " + h + "px\" />", 0, 0, 0, 1);
                    });
                    return false;
                }
            });
        });

    };
})(jQuery);
