﻿/* Global Variables */
var _siteRoot = window.location.protocol + "//" + window.location.host;

/* Media Selector */
/* insert_image() - inserts an image into the text editor
editorID - the id of the text editor
mediaID - the database id of the media item
imageLoc - the location of the image - virtual
full - signifies if full path or virtual path is needed
*/
function insert_image(editorID, mediaID, imageLoc, full) {
    var editor = get_editor(editorID);
    var imgSrc = (full == 'true') ? _siteRoot + imageLoc : imageLoc;
    var url = "<img src=\"" + imgSrc + "\" border=\"0\" alt=\"MediaLibrary#" + mediaID + "\" id=\"pods_site_img\" />";
    
    if (editor == null) {  
		alert("Could not find the edit window:" + editorID);
        return;
    }
    editor.pasteHtml(url);
    openMediaLibrarySelector();
}

/* insert_video() - inserts a video into the text editor
editorID - the id of the text editor
id - an id for the video player
img - the location of the image - virtual
imgtxt - alt value for the image
vid - the path to the video - virtual
full - signifies if full path or virtual path is needed
*/
function insert_video(editorID, id, img, imgtxt, vid, full) {
	var imgstr = '';
	var editor = get_editor(editorID);
	var height = '150';
	var width = '200';
	var imgSrc = (full == 'true') ? _siteRoot + imageLoc : imageLoc;
	var vidSrc = (full == 'true') ? _siteRoot + vid : vid;
	
	if (img != '')	{
		imgstr = "<img src='"+imgSrc+"' alt='"+imgtxt+"' />";
	}
    if (editor == null)     {
        alert("Could not find the edit window:" + editorID);
        return;
    }
    if ( editor.getSelectionHtml().length > 0 )    {
        alert("Please don't select text for this function");
        return;
    }
    if ($('#txtMSWidth') != null)    {
		width = $('#txtMSWidth').val();
    }
     if ($('#txtMSHeight') != null)    {
		height = $('#txtMSHeight').val();
    }
    
    var url = "<a href='"+vidSrc+"' style='display:block;width:"+width+"px;height:"+height+"px' id='flvplayer_"+id+"'>"+imgstr+"</a><script type='text/javascript'>flowplayer('flvplayer_"+id+"', '/files/flowplayer.swf', {clip : { autoPlay:false}});</script>";
    editor.pasteHtml(url);
    openMediaLibrarySelector()
}

/* insert_media_link() - inserts a video into a field
fieldID - the id of the text editor
mediaID - the database id for the media
full - signifies if full path or virtual path is needed
*/
function insert_media_link(fieldID, mediaID, full)
{
    var field = $('#'+fieldID);
    var mediaSrc = (full == 'true') ? _siteRoot + "/MediaItem/" + mediaID : "/MediaItem/" + mediaID;
    
    if (field != null) 
    {
        field.val(mediaSrc);
    }
    openMediaLibrarySelector();
}

/* create_link() - inserts a video into the text editor
editorID - the id of the text editor
mediaID - the database id for the media
newWindow - signifies if the link opens to a new window
full - signifies if full path or virtual path is needed
*/
function create_link(editorID, mediaID, newWindow, full)
{
    var editor = get_editor(editorID);
    var mediaSrc = (full == 'true') ? _siteRoot + "/MediaItem/" + mediaID : "/MediaItem/" + mediaID;
    
    if (editor == null) 
    {
        alert("Could not find the edit window:" + editorID);
        return;
    }
    if (editor.getSelectionHtml().length == 0 )
    {
        alert("Please select some text first");
        return;
    }
    //var url = "<a href=\"javascript:OpenMediaFile('" + mediaID + "')\">" + editor.getSelectionHtml() + "</a>";
    //var url = "<a href=\"javascript:openWindow('/pages/Media/MediaGet.aspx?id=" + mediaID + "', 'media', '')\">" + editor.getSelectionHtml() + "</a>";
    var url = "<a href=\"" + mediaSrc + "\" " + ((newWindow=='true')?"target=\"_blank\"" : "")  + ">" + editor.getSelectionHtml() + "</a>";
    editor.pasteHtml(url);
    openMediaLibrarySelector();
}

function create_generic_link(editorID, link, type)
{
    var editor = get_editor(editorID);
    if (editor == null) 
    {
        alert("Could not find the edit window:" + editorID);
        return;
    }
    if (editor.getSelectionHtml().length == 0 )
    {
        alert("Please select some text first");
        return;
    }
    //var url = "<a href=\"javascript:OpenMediaFile('" + mediaID + "')\">" + editor.getSelectionHtml() + "</a>";
    var url = "<a href=\"" + link + "\">" + editor.getSelectionHtml() + "</a>";
    editor.pasteHtml(url);
    
    if (type == "page")
    {
		openPageSelector();
    }
}

function select_media(editorID, thumbID, imageLoc) 
{
    var editor = get_object(editorID);
    var thumb = get_object(thumbID);
    
    editor.value = imageLoc;
    thumb.src = imageLoc;
    thumb.style.display = "block";
}
/* End Media Selector */

/* Gallery Selector */
function insert_gallery(editorID, flashvars, width, height, link) 
{
	var editor = get_editor(editorID);
	var url = "";
    if (editor == null) 
    {
        alert("Could not find the edit window:" + editorID);
        return;
    }
    if ( editor.getSelectionHtml().length > 0 )
    {
        //alert("Please don't select text for this function");
         url = "<a href=\"" + link + "\">" + editor.getSelectionHtml() + "</a>";
    }
    else
    {
		url = "<embed type=\"application/x-shockwave-flash\" src=\"/files/gallery.swf\" width=\"" + width + "\" height=\"" + height + "\" id=\"pods_site_gallery\" name=\"gallery\" bgcolor=\"#ffffff\" quality=\"high\" scale=\"exactfit\" menu=\"true\" FlashVars=\"" + flashvars + "\" align=\"top\" salign=\"lt\" allowfullscreen=\"true\" />";
    }
    
    editor.pasteHtml(url);
    openGallerySelector();
}

function insert_gallery_smugmug(editorID, flashvars, width, height) 
{
	var editor = get_editor(editorID);
    if (editor == null) 
    {
        alert("Could not find the edit window:" + editorID);
        return;
    }
    if ( editor.getSelectionHtml().length > 0 )
    {
        alert("Please don't select text for this function");
        return;
    }
    
    //var url = "<object align=\"middle\" height=\"" + height + "\" width=\"" + width + "\"><param name=\"movie\" value=\"http://www.smugmug.com/ria/ShizamSlides-2007090601.swf\"><param name=\"flashvars\" value=\""+flashvars+"\"><param name=\"wmode\" value=\"transparent\"><param name=\"bgcolor\ value=\"000000\"><param name=\"allowNetworking\" value=\"all\"><param name=\allowScriptAccess\" value=\"always\"><embed src=\"http://www.smugmug.com/ria/ShizamSlides-2007090601.swf\" flashvars=\""+flashvars+"\" wmode=\"transparent\" type=\"application/x-shockwave-flash\" allowScriptAccess=\"always\" allowNetworking=\"all\" height=\""+height+"\" width=\""+width+"\"></embed></object>";
    var url = "<embed src=\"http://www.smugmug.com/ria/ShizamSlides-2007090601.swf\" flashvars=\""+flashvars+"\" wmode=\"transparent\" type=\"application/x-shockwave-flash\" allowScriptAccess=\"always\" allowNetworking=\"all\" height=\""+height+"\" width=\""+width+"\"></embed>";
    editor.pasteHtml(url);
    openGallerySelector();
}
/* End Gallery Selector */

/* DEPRECATED */
function get_object(obj)
{
    if (document.getElementById) return document.getElementById(obj);
    else if (document.layers) return document.layers[obj];
    else if (document.all) return document.all[obj];
}

function show_div(obj)
{
	if (obj) obj.style.display = 'block';
}

function close_div(obj)
{
	if (obj) obj.style.display = 'none';
}


function toggle_div(name)
{
	//var div = get_object(name);
	var div = $(name);
	if ($(name).style.display == 'none') 
	{
		show_div(div);
	}
	else 
	{
		close_div(div);
	}
}
/* DEPRECATED */

function showItem(name, show)
{
	var item = get_object(name);

	if (show)
	{
		item.style.display = "block";
	}
	else
	{
		item.style.display = "none";
	}
}

function showItemByValue(curValue, name, compareValue, equal)
{
	var item = get_object(name);
	
	if (equal)
	{
		item.style.display = (curValue == compareValue) ? "block" : "none";
	}
	else
	{
		item.style.display = (curValue != compareValue) ? "block" : "none";
	}
}

function confirmDelete(msg)
{
	return confirm(msg); 
}

function openMediaLibrarySelector()
{
	if ($('#divPageSelector') != null) 
	{
		$('#divPageSelector').hide();
	}
	
	$('#divGallerySelector').hide();
	$('#divFloatEditorMenu').hide();
	$('#divFloatEditorMenu').toggle();
	$('#divMediaSelector').toggle();
}

function openGallerySelector()
{
	if ($('#divPageSelector') != null) 
	{
		$('#divPageSelector').hide();
	}
	
	$('#divMediaSelector').hide();
	$('#divFloatEditorMenu').hide();
	$('#divFloatEditorMenu').toggle();
	$('#divGallerySelector').toggle();
}

function openPageSelector()
{
	if ($('#divMediaSelector') != null) 
	{
		$('#divMediaSelector').hide();
	}
	if ($('#divGallerySelector') != null) 
	{
		$('#divGallerySelector').hide();
	}
	
	$('#divFloatEditorMenu').hide();
	$('#divFloatEditorMenu').toggle();
	$('#divPageSelector').toggle();
}

function switch_tabs(curTab, newTab, curDiv, newDiv, activeStyle, inactiveStyle)
{
	$('#'+curTab).removeClass(activeStyle);
	$('#'+curTab).addClass(inactiveStyle);
	$('#'+curDiv).hide();
	$('#'+newTab).removeClass(inactiveStyle);
	$('#'+newTab).addClass(activeStyle);
	$('#'+newDiv).show();

	//get_object(curTab).setAttribute("class", inactiveStyle);
	//get_object(curTab).setAttribute("className", inactiveStyle);
	//get_object(newTab).setAttribute("class", activeStyle);
	//get_object(newTab).setAttribute("className", activeStyle); 
	
	//close_div(get_object(curDiv));
	//show_div(get_object(newDiv));
}

function setupTab(tabHeader_i, tabContainer_i, tabBodyClass_i){
	$('#'+tabContainer_i+' div.'+tabBodyClass_i).hide(); // Hide all divs
	$('#'+tabContainer_i+' div.'+tabBodyClass_i+':first').show(); // Show the first div
	$('#'+tabHeader_i+' ul li:first').addClass('active'); // Set the class of the first link to active
	$('#'+tabHeader_i+' ul li a').click(function(){ //When any link is clicked
		$('#'+tabHeader_i+' ul li').removeClass('active'); // Remove active class from all links
		$(this).parent().addClass('active'); //Set clicked link class to active
		var currentTab = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
		$('#'+tabContainer_i+' div.'+tabBodyClass_i).hide(); // Hide all divs
		$(currentTab).show(); // Show div with id equal to variable currentTab
		return false;
	});
}

function remove_icon(txtbox_i, img_i)
{
	var txt = get_object(txtbox_i);
	var img = get_object(img_i);

	txt.value = '';
	img.src = '';
	img.style.display = 'none';
}

function getElementsByClassName(className, tag, elm)
{
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

function openWindow(url,name,options)
{
	var newWindow = window.open(url,name,options);
	newWindow.focus();
}

function closeEdit(isModal, backUrl)
{
	if(isModal)
	{
		CloseOnReload(); 
		return false;
	}
	else
	{
		window.location = backUrl;
	}
}

function getWidth()
{
      var x = 0;
      if (self.innerHeight)
      {
              x = self.innerWidth;
      }
      else if (document.documentElement && document.documentElement.clientHeight)
      {
              x = document.documentElement.clientWidth;
      }
      else if (document.body)
      {
              x = document.body.clientWidth;
      }
      return x;
}

function getHeight()
{
      var y = 0;
      if (self.innerHeight)
      {
              y = self.innerHeight;
      }
      else if (document.documentElement && document.documentElement.clientHeight)
      {
              y = document.documentElement.clientHeight;
      }
      else if (document.body)
      {
              y = document.body.clientHeight;
      }
      return y;
}

function set_field_value(field, value)
{
	if ($('#'+field) != null)
	{
		$('#'+field).val(value);
	}
}

function set_field_value(field1, value, field2)
{
	if ($('#'+field1) != null)
	{
		var val = value.replace('[LINK_TEXT]', $('#'+field2).val());
		$('#'+field1).val(val);
	}
}

function generate_loading(text_i, delay_i, cont_i) {
	$('#loading_mask').append("<div class=\"mask_content\"><img src=\"/images/interface/loader.gif\" alt=\"loader\" /><div class=\"mask_text\"></div></div>");
	
	//Get the screen height and width  
	var maskHeight = $(document).height();  
	var maskWidth = $(window).width();  
	
	if (cont_i != null)	{
		maskHeight = $('#'+cont_i).height();
		maskWidth = $('#'+cont_i).width();
		
		$('#loading_mask').css('top',  $('#'+cont_i).css('top'));
		$('#loading_mask').css('left', $('#'+cont_i).css('left'));
	}
	
	//Set height and width to mask to fill up the whole screen  
	$('#loading_mask').css({'width':maskWidth,'height':maskHeight});  

	//transition effect       
	//$('#loading_mask').fadeIn(50);      
	$('#loading_mask').fadeTo("fast",0.8);    

	$('#loading_mask div.mask_text').html(text_i); 
	//$('#loading_mask div.mask_content').css('top', maskHeight/2 - $('#loading_mask div.mask_content').height()/2);
	$('#loading_mask div.mask_content').css('top', 200);
	$('#loading_mask div.mask_content').css('left', maskWidth/2 - $('#loading_mask div.mask_content').width()/2);
	
	if (delay_i != null)	{
		setTimeout('$(\'#loading_mask\').hide()', delay_i);
	}
}

function close_loading() {
	if ($('#loading_mask').length) {
		$('#loading_mask').hide();
	}
}

/* Toggle the display of a div - attached to the Hide button on the portal panels */
function ToggleDivDisplay(name1, name2, sender) 
{
	var div1 = document.getElementById(name1);
	var div2 = document.getElementById(name2);
	if ( div1 != null && div1.style.display == 'none' ) div1.style.display = 'block';
	else if ( div1 != null ) div1.style.display = 'none';
	if ( div2 != null && div2.style.display == 'none' ) div2.style.display = 'block';
	else if ( div2 != null ) div2.style.display = 'none';
	if (sender)
	{
		if (sender.innerHTML == "–") sender.innerHTML = "+";
		else if (sender.innerHTML == "+") sender.innerHTML = "–";
	}
}

function SlideToggleDivDisplay(id, height, openText, closeText, sender) 
{
	if (height)
	{
		$('#'+id).slideToggle(height);
	}
	else
	{
		$('#'+id).slideToggle();
	}

	if (sender)
	{
		sender.innerHTML = (sender.innerHTML == openText) ? closeText : openText;
	}
}

// The id will need a # before its name. ie. #somenamehere
function getObjectTop(id) 
{
	// get the top of the content
	var top = $(id).css('top');
	return trimPx(top);
}

// Gets the height, including padding
function getObjectHeight(id) 
{
	var height = $(id).height();
	var paddingTop = trimPx($(id).css("padding-top"));
	var paddingBottom = trimPx($(id).css("padding-bottom"));

	return height + paddingTop + paddingBottom;
}

// Removes "px" from values
function trimPx(value) {
	var pos = value.indexOf("px");
	if (pos != 0)
		return parseInt(value.substring(0, pos));
	else
		return 0;
}

/* for any extra floating items */
var _PODS_floater1_name;
var _PODS_floater2_name;
var _PODS_floater3_name;
var _PODS_floater1_loc = null;
var _PODS_floater2_loc = null;
var _PODS_floater3_loc = null;

function enableFloatingItem(name, num)
{
	
	if (num == 1)
	{
		_PODS_floater1_name = '#'+name;
	}
	else if (num == 2)
	{
		_PODS_floater2_name = '#'+name;
	}
	else
	{
		_PODS_floater3_name = '#'+name;
	}
}

/* for floating gallery and media library div */
var _PODS_editorMenuName = "#divFloatEditorMenu";
var _PODS_editorMenuYloc = null;
/* for floating CMS menu div */
var _PODS_CMSMenuName = "#AdminMenu";
var _PODS_CMSMenuYloc = null;

$(document).ready(function(){
	if ($(_PODS_editorMenuName).length) _PODS_editorMenuYloc = parseInt($(_PODS_editorMenuName).css("top").substring(0,$(_PODS_editorMenuName).css("top").indexOf("px")));
	if ($(_PODS_CMSMenuName).length) 	_PODS_CMSMenuYloc = parseInt($(_PODS_CMSMenuName).css("top").substring(0,$(_PODS_CMSMenuName).css("top").indexOf("px")));
	if (_PODS_floater1_name != null && $(_PODS_floater1_name).length) _PODS_floater1_loc = parseInt($(_PODS_floater1_name).css("top").substring(0,$(_PODS_floater1_name).css("top").indexOf("px")));
	if (_PODS_floater2_name != null && $(_PODS_floater2_name).length) _PODS_floater2_loc = parseInt($(_PODS_floater2_name).css("top").substring(0,$(_PODS_floater2_name).css("top").indexOf("px")));
	if (_PODS_floater3_name != null && $(_PODS_floater3_name).length) _PODS_floater3_loc = parseInt($(_PODS_floater3_name).css("top").substring(0,$(_PODS_floater3_name).css("top").indexOf("px")));
	$(window).scroll(function () { 
		if ($(_PODS_editorMenuName).length) {
			offsetEditor = _PODS_editorMenuYloc+$(document).scrollTop()+"px";
			$(_PODS_editorMenuName).animate({top:offsetEditor},{duration:500,queue:false});
		}
		if ($(_PODS_CMSMenuName).length) {
			offsetCMS = _PODS_CMSMenuYloc+$(document).scrollTop()+"px";
			$(_PODS_CMSMenuName).animate({top:offsetCMS},{duration:500,queue:false});
		}
		if ($(_PODS_floater1_name).length)
		{
			offsetFloater1 = _PODS_floater1_loc+$(document).scrollTop()+"px";
			$(_PODS_floater1_name).animate({top:offsetFloater1},{duration:500,queue:false});
		}
		if ($(_PODS_floater2_name).length)
		{
			offsetFloater2 = _PODS_floater2_loc+$(document).scrollTop()+"px";
			$(_PODS_floater2_name).animate({top:offsetFloater2},{duration:500,queue:false});
		}
		if ($(_PODS_floater3_name).length)
		{
			offsetFloater3 = _PODS_floater3_loc+$(document).scrollTop()+"px";
			$(_PODS_floater3_name).animate({top:offsetFloater3},{duration:500,queue:false});
		}
	});
}); 
