jQuery.noConflict();

Shadowbox.loadSkin('classic', board_template_url + 'img/shadowbox');

jQuery(function($){
    Shadowbox.init({skipSetup: true});

	$('img[hover],input[hover]').imghover();

	var loginVisible = false;
	var joinVisible = false;
	var profileVisible = false;
	var searchVisible = false;

    $("a.news-share").click(function() {
        Shadowbox.open({
            player:     'iframe',
            content:    this.href,
            height:     400,
            width:      536
        });
        return false;
    });

	if(board_action == 'user_profile')
		Shadowbox.setup($('#user-marked-documents a'), {gallery: 'user_profile'});

	// Sign in
	$('div#navigation a.link-sign_in').click(function(){
		if (loginVisible) {
			resetVisible();
		} else {
			resetVisible();
			showLogin(true, this);
		}
		this.blur();
		return false;
	});

	// Join
	$('div#navigation a.link-join').click(function(){
		if (joinVisible) {
			resetVisible();
		} else {
			resetVisible();
			showJoin(true, this);
		}
		this.blur();
		return false;
	});

	// Profile
	$('div#navigation a.link-hi_board_user_nickname').click(function(){
		if (profileVisible) {
			resetVisible();
		} else {
			resetVisible();
			showProfile(true, this);
		}
		this.blur();
		return false;
	});

	// Search
	$('div#navigation a.link-search').click(function(){
		if (searchVisible) {
			resetVisible();
		} else {
			resetVisible();
			showSearch(true, this);
		}
		this.blur();
		return false;
	});

	// Close buttons
	$('img#close_login').click(resetVisible);
	$('img#close_join').click(resetVisible);
	$('img#close_profile').click(resetVisible);
	$('img#close_search').click(resetVisible);

	// Close button hover
	$('img.button_close').hover(function(){
		this.src = board_template_url + 'img/close_hover.gif';
	},function(){
		this.src = board_template_url + 'img/close.gif';
	});

	// Show functions
	function showLogin (state, link) {
		if (state) {
			$(link).addClass('active').parent().parent().addClass('active')
				.parent().next().next().slideDown();
			//$('div#block-1402').slideDown();
		} else {
			$(link).removeClass('active')
				.parent().parent().parent()
				.next().next().slideUp();
			//$('div#block-1402').slideUp();
		}
		loginVisible = state;
	}

	function showJoin (state, link) {
		if (state) {
			$(link).addClass('active').parent().parent().addClass('active')
				.parent().next().next().next().slideDown();
			//$('div#block-1403').slideDown();
		} else {
			$(link).removeClass('active')
				.parent().parent().parent()
				.next().next().next().slideUp();
			//$('div#block-1403').slideUp();
		}
		joinVisible = state;
	}

	function showProfile (state, link) {
		if (state) {
			$(link).addClass('active')
				.parent().parent().parent()
				.next().next()
				.slideDown();
			//$('div#block-1431').slideDown();
		} else {
			$(link).removeClass('active')
				.parent().parent().parent()
				.next().next()
				.slideUp();
			//$('div#block-1431').slideUp();
		}
		profileVisible = state;
	}

	function showSearch (state, link) {
		if (state) {
			$(link).addClass('active');
			$('div#searchform').slideDown();
		} else {
			$(link).removeClass('active');
			$('div#searchform').slideUp();
		}
		searchVisible = state;
	}

	// Reset menu
	function resetVisible() {
		if (loginVisible) showLogin(false,'div#navigation a.link-sign_in');
		if (joinVisible) showJoin(false,'div#navigation a.link-join');
		if (profileVisible) showProfile(false,'div#navigation a.link-hi_board_user_nickname');
		if (searchVisible) showSearch(false,'div#navigation a.link-search');

		// remove background from sign in/join menu
		$('div#navigation div:eq(2) ul').removeClass('active');
	}

	// Smaller divider in the middle of sign in/join menu
	if ($('div#navigation div:eq(2) li').length > 1) {
		$('div#navigation div:eq(2) li:eq(0)').css('backgroundImage','url('+board_template_url+'img/menu_divider_small.gif)');
	}

	// Other mouseovers
	$('input#submit_poll').hover(function(){
		this.src = board_template_url + 'img/button_stem_hover.gif';
	},function(){
		this.src = board_template_url + 'img/button_stem.gif';
	});

	$('div.block-type-concepts li').hover(function(){
		$(this).addClass('hover');
	},function(){
		$(this).removeClass('hover');
	}).click(function(){
		location.href = $(this).find('a').attr('href');
	});

	$('html.list_documentmessages form#form-insert-documentmessage input#submit-btn').hover(function(){
		$(this).css('backgroundImage','url('+board_template_url+'img/btn_submit_hover.gif)');
	},function(){
		$(this).css('backgroundImage','url('+board_template_url+'img/btn_submit.gif)');
	});

	if (!($.browser.msie && $.browser.version < 7))
		$('div#content').prepend('<div class="__content_shadow"></div>');


	$('div.block-type-archive ol.topiclist li.block-type-archive,div.block-type-forum-tracker ol.topiclist li.block-type-forum-tracker').click(function(){
		location.href = $(this).find('a').attr('href');
	});
});

;(function($){

	$.fn.pngfix = function () {	
		if ($.browser.msie && $.browser.version < 7) {
			return this.each (function () {
				$(this).css({
					filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.src+"')"
				});
				this.src = 'img/blank.gif';
			});
		} else {
			return this;
		}
	};

	$.prefetch = function() {
		for(var i = 0; i<arguments.length; i++)  {
			$("<img>").attr("src", arguments[i]);
		}
	}

	$.fn.imghover = function () {
		return this.each (function () {
			$(this).data('__img__original', this.src);
			$.prefetch($(this).attr('hover'));
		}).hover(function(){
			this.src = $(this).attr('hover');
		},function(){
			this.src = $(this).data('__img__original');
		});
	};

})(jQuery);

function openShareWindow(document_id) {	
    Shadowbox.open({
        player:     'iframe',
        content:    board_script_url + '/tell_a_friend/' + document_id + '?original_uri=' + window.location.pathname,
        height:     480,
        width:      536
    });
}

function hideDocumentMessages () {
	jQuery('iframe#iframe-picture-replies').hide();
}

function launchDetachedPlayer() {
  var url = "";
  var i = location.href.indexOf("#");
  var medium = ""; //first word in vpath; 
  if(i == -1) {
    // get medium from base url
    var media = ["video", "audio"];
    for(var m in media) {
      if(location.href.indexOf("/" + media[m] + "/") != -1) {
        medium = media[m];
      }
    }
    url = "/templates/qdance/DetachedPlayer/" + medium + "/index.html";
  } else {
    var vpath = location.href.substr(i + 1);
    var parts = vpath.split("/");
    medium = parts[1];
//    alert(medium);
    url = "/templates/qdance/DetachedPlayer/" + medium + "/index.html#" + vpath;
  }
  var options;
  if(medium == "video") {
    options = 'width=970, height=486';
  }
  if(medium == "audio") {
    options = 'width=970, height=314';
  }
  var newwin = window.open(url, 'qdancedetached', options);
  newwin.focus();
}

