﻿/// <reference path="jquery-1.3.2.min.js"/>

var pageTracker;
var defaultFile = "default.aspx";

// google analytics tracking code
jQuery(document).ready(function($) {

    checkColorStyle();
    //checkLiveBooks();

    var gaTrackCode = "UA-10183191-2"; //New Fritz Mueller Account
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");

    jQuery.getScript(gaJsHost + "google-analytics.com/ga.js", function() {
        try {
            pageTracker = _gat._getTracker(gaTrackCode);
            pageTracker._trackPageview();
        } catch (err) { }
    });

    AddTrackPageAnalytics();
    AddDocumentAnalytics();
	WireToolBox();
    Signup('a.signup');
});

jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],
{
    random: function(a, i, m, r) {
        if (i == 0) {
            jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
        };
        return i == jQuery.jQueryRandom;
    }
});

/*
* Site-functions 
*/
(function($) {   
    $.fn.stripHtml = function() {   
        var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;   
        this.each(function() {   
           $(this).html(   
                $(this).html().replace(regexp, "")
            );   
        });   
        return $(this);   
    }
})(jQuery);

/*
 * JQuery Plugin: "EqualHeights"
 * by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
 *
 * Copyright (c) 2008 Filament Group
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 */
$.fn.equalHeights = function(px) {
    $(this).each(function() {
        var currentTallest = 0;
        $(this).children(".adjust").each(function(i) {
            if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
        });
        //if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
        // for ie6, set height since min-height isn't supported
        if ($.browser.msie && $.browser.version == 6.0) { $(this).children(".adjust").css({ 'height': currentTallest }); }
        $(this).children(".adjust").css({ 'min-height': currentTallest });
    });
    return this;
};

$.fn.equalWidths = function(px) {
    $(this).each(function() {
        var currentWidest = 0;
        $(this).children().each(function(i) {
            //alert($(this).width());
            if ($(this).width() > currentWidest) { currentWidest = $(this).width(); }
        });
        //if (!px || !Number.prototype.pxToEm) currentWidest = currentWidest.pxToEm(); //use ems unless px is specified
        // for ie6, set height since min-height isn't supported
        if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({ 'width': currentWidest }); }
        $(this).children().css({ 'min-width': currentWidest });
    });
    return this;
};

$.fn.fixPNG = function() {
    return this.each(function() {
        var image = $(this).css('backgroundImage');

        if (image.match(/^url\(["']?(.*\.png)["']?\)$/i)) {
            image = RegExp.$1;
            $(this).css({
                'backgroundImage': 'none',
                'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=" + ($(this).css('backgroundRepeat') == 'no-repeat' ? 'crop' : 'scale') + ", src='" + image + "')"
            }).each(function() {
                var position = $(this).css('position');
                if (position != 'absolute' && position != 'relative')
                    $(this).css('position', 'relative');
            });
        }
    });
};

$.fn.fixTargetPNG = function(imagePath) {
    return this.each(function() {
        var image = "url(\"" + imagePath + "\")";
        
        if (image.match(/^url\(["']?(.*\.png)["']?\)$/i)) {
            image = RegExp.$1;
            $(this).css({
                'backgroundImage': 'none',
                'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=" + ($(this).css('backgroundRepeat') == 'no-repeat' ? 'crop' : 'scale') + ", src='" + image + "')"
            }).each(function() {
                var position = $(this).css('position');
                if (position != 'absolute' && position != 'relative')
                    $(this).css('position', 'relative');
            });
        }
    });
};

function Initializers() {
    KeyLine();
    WireMenu();
	WireGalleryList();
    InitPipeLinks();
	WireHomeSearch();
}


function InitPipeLinks() {
        $('.PipeMenu').children('a:last').addClass('NoBorder');
}

function WireHomeSearch() {
    $('#HomeSearch').children('form').children('input').each(function() {
        if ($(this).attr('type') == 'submit') {
            $(this).attr('value', 'search');
			$(this).attr('class', 'SearchButton');
        }
    });
	
	//$('#HomeSearch').children('form').children('input.search_box').attr('height', '22');
}

function WireMenu() {
    //$('li.main').removeClass('nojs');
    $('#LeftMenu').children('li:last').addClass('NoBorder');
    $('#RightMenu').children('li:last').addClass('NoBorder');
    //$('#LeftMenu li.main').siblings('li').children('div').hide();
    //$('#RightMenu li.main').siblings('li').children('div').hide();
    
    //CheckURLAndActivateMenuItem();

    $('#navItems').append('' + $('#LeftMenu').html() + "<span class=\"rightSpan\">" + $('#RightMenu').html() + "</span>");
    $('#navItems').children('li:last').addClass('NoBorder');
}

function WireGalleryList() {
	$('.slide').each(function() {
    	$(this).html('<div class="GalThumb">' + $(this).html() + '</div>');
	 });
}

function WirePhotoDisplay() {
	$imgOverlay = $('.imageWidget').children('div').children('div').children('img');
	
	var marginLeft = 0;
	if ($imgOverlay.width() < 564) marginLeft = (572-$imgOverlay.width())/2;
	
	$('#StockTeaserImage').css({ 'width': ($imgOverlay.width() + 8) + 'px', 'marginLeft': marginLeft + 'px' });
	
	// Set actual image height and width to match the gif overlay
	$actualImage = $('.imageWidget').children('div').children('img');
	$actualImage.css({ 'width': $imgOverlay.width() + 'px', 'height': $imgOverlay.height() + 'px' });
}

function ClearCurrent(target) {
    $(target).removeClass('Current');
}

/*****/

function PngFix() {
    $(function($) { $('.png').pngfix(); });
}

function AddTrackPageAnalytics() {
    $('a').each(function() {
        var $a = $(this);
        var href = $a.attr('href');

        // see if the link is external
        if (href != null && (href.match(/^http/)) && (!href.match(document.domain))) {

            // if so, add the GA tracking code
            $a.click(function() {
                pageTracker._trackPageview('/outbound/' + href);
            });
        }
    });
}

function AddDocumentAnalytics() {
    var fileTypes = ['doc', 'xls', 'docx', 'xlsx', 'pdf', 'ppt', 'zip'];

    $('a').each(function() {
        var $a = $(this);
        var href = $a.attr('href');

        if (href != null) {
            var hrefArray = href.split('.');
            var extension = hrefArray[href.length - 1];

            if ($.inArray(extension, fileTypes) != -1) {
                $a.click(function() {
                    // get the post title if there is one
                    // and add it to the string for tracking
                    pageTracker._trackPageview('/documents/' + href);
                });
            }
        }
    });
}

jQuery.preloadImages = function() {
    for (var i = 0; i < arguments.length; i++) {
        jQuery("<img>").attr("src", arguments[i]);
    }
}

function getQueryAnchorVar(str) {
    if ("undefined" == typeof (str))
        str = location.href;
    return (str.replace(/.+[\/]([^\/]+)$/, '$1'))
}

function checkColorStyle() {
    var colorStyle = readCookie('colorStyle');
    if (colorStyle) switchStylestyle(colorStyle);
}

// colour switching features
function inkit(str, target) {
    switchStylestyle(str);
    createCookie('colorStyle', str, 365);

    KeyLine();
}

function switchStylestyle(styleName) {
    var fileref = document.createElement('link');
    fileref.setAttribute('rel', 'stylesheet');
    fileref.setAttribute('type', 'text/css');
    fileref.setAttribute('href', '/AppStyle/color' + styleName + '.css');
    document.getElementsByTagName('head')[0].appendChild(fileref);
}

// Function to scale out the left and right keylines for longer pages.
function KeyLine() {
    var containedHeight = "";
    if ($('#contentContainer').length != 0) { containedHeight = $('#contentContainer').outerHeight(); }
    else if ($('#homeContentContainer').length != 0) { containedHeight = ($('#homeContentContainer').outerHeight()+25); }
    
    if (containedHeight != null) {
        $('#leftLineIn').css({ 'height': (containedHeight - 2) + 'px' });
        $('#leftLineOut').css({ 'height': (containedHeight + 69) + 'px' });
        $('#rightLineIn').css({ 'height': (containedHeight - 2) + 'px' });
        $('#rightLineOut').css({ 'height': (containedHeight + 1) + 'px' });

        // Required to address transparent PNG fade-in issue in IE browsers
        if ($.browser.msie) {
            var colorStyle = readCookie('colorStyle');
            
			if (colorStyle == 'Gray') $('.gradient').fixTargetPNG('http://outcropyukon.com/fritzAssets/skin/gradient-gray.png');
            else $('.gradient').fixTargetPNG('http://outcropyukon.com/fritzAssets/skin/gradient-white.png');
        }
        $('.gradient').css({ 'height': (containedHeight) + 'px' }).fadeIn('slow');
    }
}

function HighlightStories() {
    $('.ArticleItem').each(function() {
        $(this).mouseenter(function() {
            $(this).addClass('highlight');
        });
        $(this).mouseleave(function() {
            $(this).removeClass('highlight');
        });
    });
}

// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}
function eraseCookie(name) {
    createCookie(name, "", -1);
}
// /cookie functions

function WireToolBox() {
 $('#ToolSet .toolTip').hide();//.css('display','none');
 // hides all subnavigation

  $('#ToolSet li.tool').hover( //
        function() {
        //slidedown subnavigation
        // $('.toolTip',this).css('display','block');
      $('.toolTip',this).show();//.slideDown({ speed: 'slow', easing: 'easeOutBack' }); 
        },
        function() {
			$('.toolTip', this).hide();
        }
    );
 }
 
 function Signup(target) {
    $(target).fancybox({
        'frameWidth': 600,
        'frameHeight': 350, 
        'centerOnScroll': true,
		'hideOnContentClick': false,
		'zoomOpacity': true,
		'zoomSpeedIn': 600,
		'zoomSpeedOut': 500
	});
	//'type': 'iframe'toptions="group = links, type = iframe, effect = fade, width = 600, height = 475, overlayClose = 1, layout = quicklook" 
}
