
// get root path 
function getRootPath (serverRoot) {
	var root = '';
	var fullPath = '';
	var m = window.location.href.match ('^(?:(https?|ftp|file)://)?([a-z0-9-]+(?:\.[a-z0-9-]+)+)?(.*?)?(?:(\w+\.\w+)([^.]*))?$');
	if (m && m.length >= 3)
		fullPath = m[2];
	if (fullPath.length) {
		if (serverRoot)
			fullPath = fullPath.replace (serverRoot, '');
		var pathArray = fullPath.split ('/');
		if (pathArray.length > 1)
			root = pathArray [0];
	}
	return (root.toLowerCase());
}

// Image pop up
function openModalDialog (url, width, height, useIFrame, idDiv) {

		var div = null;

		if (!idDiv)
			var div = $('<div id="modal-window"></div>').appendTo ('body');
		else
			div = $('#' + idDiv);

		if (!div.length)
			alert ('specified div: ' + idDiv + ' not found');

		$(div).dialog ({
			width: width,
			height: height,
			modal: true,
			title: 'Slide Show',
			resizable: false,
			open: function (event, ui) {
				if (useIFrame === true) {
					var iFrame = $('<iframe src="' + url + '" class="dlg-iframe"></iframe>').appendTo (div);
				}
				else {
					$(div).html ('<div class="ajax-loader"></div>');
					$.ajax({
						url: url,
						dataType: 'html',
						success: function (html) {
							$(div).html (html);
						},
						error: function (jqXHR, textStatus, errorThrown) {
							$(div).html ('An Error Occurred!');
						}
					});
				}
			},
			close: function () {
				if (idDiv.length)
					$(div).html ('');
				else
					$(div).remove ();
			}
		});
	}
// set columb height to be equal
function equalHeight(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}


        function loggedin(username) {
			window.location.reload();
        }
        function loggedout() {
			window.location.reload();
        }


$(document).ready(function() {

	// add link to background for buyout
	$("img.skin_ad").click(function(e){
		// open link in new tab
		window.open ($(this).attr("rel"), '_blank');
	    // open link in same window
	    // window.location = $(this).attr("rel");   
	});

	
	// Style forms on site
	$('.tgrElem').jqTransform({imgPath:'/theme/tgr40/css/img/'});

    /* Initialize Menu */
    $("#menu").megaMenu('hover_fade');

    /* Apply active nav class */
    //var root = getRootPath('staging.tetongravity.com/');
   // $('#mn_' + root).addClass('active');

    // alert ('root: ' + root);

	/*
	and id or class with equal height set here
	
	equalHeight($(".recent-article"));
	equalHeight($(".footer-col"));
	
	*/
	
	/* Login Drop down */
	$("div.panel_button").click(function(){
		$("div#panel").animate({
			height: "160px"
		})
		.animate({
			height: "140px"
		}, "fast");
		$("div.panel_button").slideToggle("medium");	
	});	
	
    $("div#hide_button").click(function(){
		$("div#panel").animate({
			height: "0px"
		}, "fast");
    });	
   
   /* Hide Search tokens on click */
   $("#token_list ul#tokens li a").click(function(){
   		$(this).slideToggle("medium");	
   });	
   
    /* List or grid view swap tool - defaults to list view */
    $("a.switch_thumb").click(function () {
        if ($(this).hasClass('swap')) {
            //changes to thumb view
            $(this).removeClass("swap");
            $("ul.display").fadeOut("fast", function () {
                $(this).fadeIn("fast").addClass("thumb_view");
                $.get('/save_grid_list_view.ashx?list=false');
            });
        }
        else {
            //changes to list view
            $(this).addClass("swap");
            $("ul.display").fadeOut("fast", function () {
                $(this).fadeIn("fast").removeClass("thumb_view");
                $.get('/save_grid_list_view.ashx?list=true');
            });
        }
        return false;
    });

    $('a.media_pop').each(function () {
        $(this).click(function () {
            var url = '/theme/tgr40/iframe_pages/media_pop_display.aspx';
            var mediaType = $(this).attr('media_type');
            if (!mediaType || mediaType == 'undefined' || mediaType == '')
                mediaType = '';

            url += '?mediaType=' + mediaType + '&mediaID=' + $(this).attr('rel') + '&click=' + $(this).attr('clicked_rel');
            openModalDialog(url, 950, 550, true);
        });
    });
    

  
    
 });

$(function(){
				
	// Tabs

//	$('#fb_twitter_tabs').tabs();
//	$('#featured_scroll_tabs').tabs();
//	$('#forum_talk_stories_tabs').tabs();

				
});

	function openForumThread (threadID) {
		//alert ('threadID: ' + threadID);
		var url = '/gear-store/dyn/forum-thread-reader.html?threadID=' + threadID;
		var dlg = $('#forum-dialog');
		if (!dlg.length)
			dlg = $('<div id="forum-dialog" class="tgr-modal-dialog"></div>').appendTo ('body');
		
		$(dlg).dialog ({
			width: 800,
			height: 600,
			title: 'Forum Reader', 
			open: function () {
				$(dlg).html ('<iframe class="forum-dialog-iframe" src="' + url + '"></iframe>');
			}
		});
	}
