// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function show_spinner(section) {
	new Effect.Opacity(section, { from: 1.0, to: 0.3, duration: 0.7 });
	new	Effect.toggle('loading', 'appear');
}

function hide_spinner(section) {
	new Effect.Opacity(section, { from: 0.3, to: 1, duration: 0.7 });
	new	Effect.toggle('loading', 'appear');
}

// Detection of when any ajax request is posted
var overlayTimeout;

Ajax.Responders.register({
  onCreate: function() {
    if($('overlay') && Ajax.activeRequestCount > 0)
      overlayTimeout = Effect.Appear.delay(0.5, 'overlay', {duration:0.2, queue:'end'});
  },
  onComplete: function() {
    if($('overlay') && Ajax.activeRequestCount == 0) {
      window.clearTimeout(overlayTimeout);
      Effect.Fade('overlay', {duration:0.2, queue:'end'});
    }
  }
});

// OK, all of the hidable things
var motd_show = getCookie('motd_show');
if (motd_show.length == 0) { motd_show = 'yes'; }

var motd_date = getCookie('motd_date');
if (motd_date.length == 0)
{ 
	var curdate = new Date();
	motd_date = curdate.toGMTString();
}

var support_show = getCookie('support_show');
if (support_show.length == 0) { support_show = 'yes'; }

var other_clip_sites_show = getCookie('other_clip_sites_show')
if (other_clip_sites_show.length == 0) { other_clip_sites_show = 'yes'; }

var other_collect_sites_show = getCookie('other_collect_sites_show')
if (other_collect_sites_show.length == 0) { other_collect_sites_show = 'yes'; }


function replacePreviewLink(uuid, link, title, desc, width, height)
{
	var rel = "";
	//var agent=navigator.userAgent.toLowerCase();
	//var is_iphone = ((agent.indexOf('iphone')!=-1);
		if (detectQuickTime())
		{
			link = link + ".m4v";
			rel="quicktime"
		} else 
		{
			if (detectFlash()) 
			{
				filename = "/media/videoPlayer-16x9.swf";
				if (height > 360)
				{
					filename = "/media/videoPlayer-4x3.swf";
				}
				link = filename + "?skin=/media/SkinOverPlaySeekStop.swf&skinAutoHide=true&movie=" + link + ".m4v"
				rel = "flash"
			}
		}
	if (rel != "")
	{
		var build = title + ' :: ' + desc + ':: width:' + width + ', height:' + height + ', topclose: true, menubar: top';
		$(uuid).setAttribute('href',link);
		$(uuid).setAttribute('class', "lightview");
		$(uuid).setAttribute('rel', rel);
		$(uuid).setAttribute('title', build);
	}
}

function process_cart_items_from(list)
{
	var result = "";
	for (var j=0; j<cart_button_list.length; j++)
	{
		if ($(cart_button_list[j]['name']) != undefined)
		{
			if (list.indexOf(cart_button_list[j]['name']) > -1)
			{
				result = cart_button_list[j]['remove'];
			} else
			{
				result = cart_button_list[j]['add'];
			}
			$(cart_button_list[j]['name']).innerHTML = result;
		}
	}
}



function make_item_purchasable(name)
{
	buy_name = "buy_" + name;
	remove_name = "remove_" + name;
	
	if ($(buy_name) != undefined)
	{
		$(buy_name).show();
		$(remove_name).hide();
	}
}

function make_item_removable(name)
{
	buy_name = "buy_" + name;
	remove_name = "remove_" + name;
	
	if ($(buy_name) != undefined)
	{
		$(buy_name).hide();
		$(remove_name).show();
	}
}

function initial_code()
{
}


// Controls Collapsing and expanding of sections
// Code from http://www.kleenecode.net/2008/03/01/valid-and-accessible-collapsible-panels-with-scriptaculous/

function attachCollapsible(collapsible)
{
    var div = $(collapsible);
    var heading = div.select('h4')[0];
    var contents = div.select('div')[0];

    // initially hide it
    div.addClassName('closed');
    contents.hide();

    // add an anchor at run time,
    // that way non-js users won't see anchor, but js users will
    // still be able to use their keyboard for it.
    heading.innerHTML =
            '<a href="#">' + heading.innerHTML + '</a>';
    heading.firstDescendant().onclick =
            collapsibleOnClick(div, heading, contents);
}

function collapsibleOnClick(div, heading, contents)
{
    return function() {
        // clear the height; gets messy otherwise if
        // a blind is already in progress
        // not perfect, but simple and huffy enough,
        // and doesn't seem to be able to get into a bad state
        contents.style.height = '';

        // do we need to go up or down?
        if (div.hasClassName('closed')) {
            Element.removeClassName(div,'closed');
            new Effect.BlindDown(contents,{duration:0.3, fps:100});
        } else {
             Element.addClassName(div,'closed');
           new Effect.BlindUp(contents,{duration:0.3, fps:100});
        }

        // event has been dealt with.
        return false;
    };
}

function toggleCheckboxes(listOfIds, value)
{
	for (var j=0; j<listOfIds.length; j++)
	{
		$(listOfIds[j]).checked = value;
	}
}



// Cookie Javascript Code from: http://scripts.franciscocharrua.com/javascript-cookies.php
function setCookie(name, value)
         {
         //If name is the empty string, it places a ; at the beginning
         //of document.cookie, causing clearCookies() to malfunction.
         if(name != '')
            document.cookie = name + '=' + value;
         }

function getCookie(name)
         {
         //Without this, it will return the first value 
         //in document.cookie when name is the empty string.
         if(name == '')
            return('');
         
         var name_index = document.cookie.indexOf(name + '=');
         
         if(name_index == -1)
            return('');
         
         var cookie_value =  document.cookie.substr(name_index + name.length + 1, 
                                                document.cookie.length);
         
         //All cookie name-value pairs end with a semi-colon, except the last one.
         var end_of_cookie = cookie_value.indexOf(';');
         if(end_of_cookie != -1)
            cookie_value = cookie_value.substr(0, end_of_cookie);

         //Restores all the blank spaces.
         var space = cookie_value.indexOf('+');
         while(space != -1)
              { 
              cookie_value = cookie_value.substr(0, space) + ' ' + 
              cookie_value.substr(space + 1, cookie_value.length);
							 
              space = cookie_value.indexOf('+');
              }

         return(cookie_value);
         }

function clearCookie(name)
         {                  
         var expires = new Date();
         expires.setYear(expires.getYear() - 1);

         document.cookie = name + '=null' + '; expires=' + expires; 		 
         }
         
function clearCookies()
         {
         var Cookies = document.cookie;
         var Cookie = Cookies;
         var expires = new Date();
         expires.setYear(expires.getYear() - 1);

         while(Cookie.length > 0)
              {
              //All cookie name-value pairs end with a semi-colon, except the last one.
              Cookie = Cookies.substr(0, Cookies.indexOf(';'));
              Cookies = Cookies.substr(Cookies.indexOf(';') + 1, Cookies.length);

              if(Cookie != '')
                 document.cookie = Cookie + '; expires=' + expires;
              else
                 document.cookie = Cookies + '; expires=' + expires;			  			  	  
              }		 		 
         }
        

function filter_smartlists(codecs)
		{
			var formats_allowed = new Array();
			for (var i=0; i<codecs.length; i++)
			{
				
				if ($(codecs[i].codec_id).checked)
				{
					for (var k=0; k<codecs[i].formats.length; k++)
					{
						formats_allowed.push(codecs[i].formats[k]);
					}
				}
			}
			var isThere;
			
			for (var j=0; j<smart_list_formats.length; j++)
			{
				isThere = (smart_list_formats[j].formats.intersect(formats_allowed).length > 0);
				
				if (isThere)
				{
					$(smart_list_formats[j].item_id).show();
				} else
				{
					$(smart_list_formats[j].item_id).hide();
				}
			}
		
		}
		
function link_to_page_from_selected_smart_list(uri, codecs)
		{
			var link = uri;
			var hasDoneFirst = false;
			for (var i=0; i<codecs.length; i++)
			{
				if ($(codecs[i].codec_id).checked)
				{
					if (!hasDoneFirst)
					{
						hasDoneFirst = true;
						link = link + '?';
					} else
					{
						link = link + '&';
					}
					link = link + 'filter_by_codec["' + codecs[i].name + '"]=' + codecs[i].name;
				}
			}			
			location.href = link;
		}
		
function assign_filter_values(form_item, codecs)
		{
			if (codecs)
			{
				var result = "";
				for (var i=0; i<codecs.length; i++)
				{
					if ($(codecs[i].codec_id).checked)
					{
						if (result.length > 0)
						{
							result = result + ",";
						}
						result = result + codecs[i].name;
					}
				}
				form_item.value = result;
			}
		}