﻿$(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");


	/*
	// 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");
	*/


	$("#DistributorInfoWindowSource .tools").live('click', function () {
		$("#DistributorInfoWindowSource").hideit();
	});
});
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;
    }
         }

/*
//for forms med label inni textbox, sørger for fadeut og inn ved klikk/focus på box
function setupForm(lableSelector, textboxSelector) {
    $(lableSelector).each(function (i, e) {
        var el = $(e);
        if (el.next().val() == "") el.fadeIn("slow");

    });
    $(textboxSelector).bind("click focus", function () {
        var prev = $(this).prev();
        if ($(this).val() == "")
            $(this).prev().fadeOut(100);
    }).blur(function () {
        if ($(this).val() == "")
            $(this).prev().fadeIn(200);
    });

}
(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);
*/

function isSet(a) {
    return (a && a.trim() != "");
}

function ShowDistributor(a) {
    $("#DistributorInfoWindowSource tr, #DistributorInfoWindowSource div, #DistributorInfoWindowSource span").show();
    
    if (customerID == a.ContactUserID) {
        $("#DistributorInfoWindowSource .btnEditMapInfo").show();
       } else {
        $("#DistributorInfoWindowSource .btnEditMapInfo").hide();
    }
       
    $("#DistributorInfoWindowSource .header").text(a.Title.toUpperCase());
    if (isSet(a.Address)) $("#DistributorInfoWindowSource .besoksAdr").text(a.Address); else $("#DistributorInfoWindowSource .besoksAdr").parent().hide();
    if (isSet(a.PostalCode) || isSet(a.City)) $("#DistributorInfoWindowSource .poststed").text(a.PostalCode + " " + a.City); else $("#DistributorInfoWindowSource .poststed").parent().hide().prev().hide();
    if (isSet(a.Phone)) $("#DistributorInfoWindowSource .tlf").text(a.Phone); else $("#DistributorInfoWindowSource .tlf").parent().hide().prev().hide();
    if (isSet(a.Email)) $("#DistributorInfoWindowSource .epost").html('<a href="mailto:' + a.Email + '">' + a.Email + '</a>'); else $("#DistributorInfoWindowSource .epost").parent().hide().prev().hide();
    if (isSet(a.Website)) $("#DistributorInfoWindowSource .website").html('<a href="' + a.Website + '" target="_blank">' + a.Website + '</a>'); else $("#DistributorInfoWindowSource .website").parent().hide().prev().hide();
    
    if (a.ContactUser) {
        a.ContactName = a.ContactUser.LastName;
        a.ContactPhone = a.ContactUser.Phone;
        a.ContactEmail = a.ContactUser.Email;
    }
       
    if (isSet(a.ContactName) || isSet(a.ContactPhone) || isSet(a.ContactEmail)) {
        $("#DistributorInfoWindowSource .contactPerson").html(a.ContactName + (isSet(a.ContactWebsiteTwitter) ? ' <a href="' + a.ContactWebsiteTwitter + '" target="_blank"><img src="/App_Themes/wondercode/images/icon_twitter2.png" alt="twitter" /></a>' : '') + (isSet(a.ContactWebsiteLinkedIn) ? ' <a href="' + a.ContactWebsiteLinkedIn + '" target="_blank"><img src="/App_Themes/wondercode/images/icon_linkedin.png" alt="LinkedIn" /></a>' : ''));
        if (isSet(a.ContactPhone)) $("#DistributorInfoWindowSource .contactPhone").text(a.ContactPhone); else $("#DistributorInfoWindowSource .contactPhone").parent().hide();
        if (isSet(a.ContactEmail)) $("#DistributorInfoWindowSource .contactEmail").html('<a href="mailto:' + a.ContactEmail + '">' + a.ContactEmail + '</a>'); else $("#DistributorInfoWindowSource .contactEmail").parent().hide();
    } else {
        $("#DistributorInfoWindowSource .contact").hide();
    }
       
    $("#DistributorInfoWindowSource .description").html(isSet(a.Description) ? a.Description : "");
    
    if (isSet(a.WebsiteYoutube) || isSet(a.WebsiteTwitter) || isSet(a.WebsiteFacebook)) {
        $("#DistributorInfoWindowSource .footer").html('<div class="hr"></div><div class="right">'
        + (isSet(a.WebsiteYoutube) ? '<a href="' + a.WebsiteYoutube + '" target="_blank"><img src="/App_Themes/wondercode/images/icon_youtube2.png" alt="youtube" /></a>' : '')
        + (isSet(a.WebsiteTwitter) ? '<a href="' + a.WebsiteTwitter + '" target="_blank"><img src="/App_Themes/wondercode/images/icon_twitter2.png" alt="twitter" /></a>' : '')
        + (isSet(a.WebsiteFacebook) ? '<a href="' + a.WebsiteFacebook + '" target="_blank"><img src="/App_Themes/wondercode/images/icon_facebook2.png" alt="facebook" /></a>' : '')
        + '</div>');
    } else {
		$("#DistributorInfoWindowSource .footer").html("");
	}
	$("#DistributorInfoWindowSource").showit();
    //TINY.box.show("<div id=\"DistributorInfoWindow\">" + $("#DistributorInfoWindowSource").html() + "</div>", 0, 550, Math.round($(window).height() * 0.9), 1);

}
