/* cookie class

Usage: 
	Cookie.init({name: 'yourdata', expires: 90});
    Cookie.setData('favorites', false);

	Expires on browser close, multiple values
	Cookie.init({name: 'mydata'}, {foo: 'bar', x: 0});
    alert(Cookie.getData('foo'));

*/

var Cookie = {
  data: {},
  options: {expires: 1, domain: "", path: "", secure: false},

init: function(options, data) {
  Cookie.options = Object.extend(Cookie.options, options || {});

  var payload = Cookie.retrieve();
        if(payload) {
            Cookie.data = payload.evalJSON();
        }
        else {
            Cookie.data = data || {};
        }
        Cookie.store();
    },
    getData: function(key) {
        return Cookie.data[key];
    },
    setData: function(key, value) {
        Cookie.data[key] = value;
        Cookie.store();
    },
    removeData: function(key) {
        delete Cookie.data[key];
        Cookie.store();
    },
    retrieve: function() {
        var start = document.cookie.indexOf(Cookie.options.name + "=");

        if(start == -1) {
            return null;
        }
        if(Cookie.options.name != document.cookie.substr(start, Cookie.options.name.length)) {
            return null;
        }

        var len = start + Cookie.options.name.length + 1;   
        var end = document.cookie.indexOf(';', len);

        if(end == -1) {
            end = document.cookie.length;
        } 
        return unescape(document.cookie.substring(len, end));
    },
    store: function() {
        var expires = '';

        if (Cookie.options.expires) {
            var today = new Date();
            expires = Cookie.options.expires * 86400000;
            expires = ';expires=' + new Date(today.getTime() + expires);
        }

        document.cookie = Cookie.options.name + '=' + escape(Object.toJSON(Cookie.data)) + Cookie.getOptions() + expires;
    },
    erase: function() {
        document.cookie = Cookie.options.name + '=' + Cookie.getOptions() + ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
    },
    getOptions: function() {
        return (Cookie.options.path ? ';path=' + Cookie.options.path : '') + (Cookie.options.domain ? ';domain=' + Cookie.options.domain : '') + (Cookie.options.secure ? ';secure' : '');      
    }
};

function switchView(key,value)
{
	Cookie.init({name: 'puzzleslayout', path: '/'});
	
	if(key == 'layout')
		if(Cookie.getData('layout')==3)
			Cookie.setData('pagesize',18);
		else
			Cookie.setData('pagesize',18);
			
	Cookie.setData(key,value);
	
	curhref = window.location.href;
	
	newhref = curhref.split("/p-");
	
	if(newhref.length > 1) {
		if(newhref[0].search("/puzzlesearch") < 0 && newhref[0].search("/search") < 0 ) window.location.reload();
		else window.location.href = newhref[0]+"/p-1/";
	}
	else window.location.reload();
}

function display_login(login_status)
{
	
            var ni = document.getElementById('loginBox');
            var newanchor = document.createElement('a');

          if (login_status)
             ni.innerHTML ="<a href='cart/my-account.php?Logout=Go&ContinueURL=/index.html' title='Login' style='color:#ffffff;text-decoration:none'>Logout</a>";
          else 
             ni.innerHTML ="<a href='cart/my-account.php?ContinueURL=/index.html' title='Login' style='color:#ffffff;text-decoration:none'>Login</a>";
          
}

function loadCatAjax(cat,div,page)
{
		
	new Ajax.Request('/category-ajax.php', 
					 {
					 	method: 'post',
		 			  	parameters: {cmd: 'category', id: cat, product: page, cont:div},
						onSuccess: function(transport)
						{
							$(div+'_update').innerHTML = transport.responseText;
								
						},
						onFailure: function(transport)
						{ 
							//alert('Failed');
							if(404 == transport.status)
							{
								$(div).innerHTML = 'Nothing Available';
							}							
						}
	 				 }
					 ); 
		new Ajax.Request('/category-ajax.php', 
					 {
					 	method: 'post',
		 			  	parameters: {cmd: 'pagination', id: cat, product: page, cont:div},
						onSuccess: function(transport)
						{
							$(div+'_header_pagination').innerHTML = transport.responseText;
								
						},
						onFailure: function(transport)
						{ 
							//alert('Failed');
							if(404 == transport.status)
							{
								$(div).innerHTML = 'Nothing Available';
							}							
						}
	 				 }
					 );
}

function resize_bg() {
	if($('iVid_bg'))
		$('iVid_bg').setStyle = {
			height: get_document_window_height()+"px"
		}
}

function showVideo(f) 
{	
   var dbody = $(document.body);
	
	var divwidth  = 560;
	var divheight = 370;

	var window_width = get_document_window_width();
   var width = convert_width(divwidth,window_width);
   var window_height = get_document_window_height();
   var height = convert_height(divheight,window_height);
   var left = calculate_left(width,window_width);
   var top = calculate_top(height,window_height);
	
	var background   = new Element('div', {id:'iVid_bg', style:'display:none;'});
	var container    = new Element('div', {id:'iVid_cont', style:'display:none;'});
	var header       = new Element('div', {id:'iVid_header'});
	var content      = new Element('div', {id:'iVid_content'});
	var header_text  = new Element('div');
	header_text.className = 'iVid_header_text';
	var header_close = new Element('div');	
	header_close.className = 'iVid_header_close';
	var close_img    = new Element('img', {src:'images/map/map_close.gif',title:"Close"});
	
	container.style.top = "150px";
	container.style.left = left+"px";
	
	background.setStyle = {
			height: get_document_window_height()+"px"
		}
	
	header_text.insert('NanoGLIDE &trade;');
	header_close.insert(close_img);
	
	header.insert(header_text);
	header.insert(header_close);
	
	content.insert(AC_FL_PreloadContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0','width','560','height','340','movie','http://www.youtube.com/v/yR0bRlOgCkA&hl=en_US&fs=1&rel=0','quality','high','allowfullscreen','false' ));
	container.insert(header);
	container.insert(content);
	dbody.insert(background);
	dbody.insert(container);
	
	background.observe('click',hideVideo);
	close_img.observe('click',hideVideo);
	
	$('iVid_bg').show();
	$('iVid_cont').show();
}
function showVideo2(f) 
{	
   var dbody = $(document.body);
	
	var divwidth  = 640;
	var divheight = 380;

	var window_width = get_document_window_width();
   var width = convert_width(divwidth,window_width);
   var window_height = get_document_window_height();
   var height = convert_height(divheight,window_height);
   var left = calculate_left(width,window_width);
   var top = calculate_top(height,window_height);
	
	var background   = new Element('div', {id:'iVid_bg', style:'display:none;'});
	var container    = new Element('div', {id:'iVid_cont', style:'display:none;'});
	container.style.width=divwidth+'px';
	var header       = new Element('div', {id:'iVid_header'});
	header.style.width=divwidth+'px';
	var content      = new Element('div', {id:'iVid_content'});
	var header_text  = new Element('div');
	header_text.className = 'iVid_header_text';
	var header_close = new Element('div');	
	header_close.className = 'iVid_header_close';
	var close_img    = new Element('img', {src:'images/map/map_close.gif',title:"Close"});
	
	container.style.top = "150px";
	container.style.left = left+"px";
	
	background.setStyle = {
			height: get_document_window_height()+"px"
		}
	
	header_text.insert('NanoGLIDE &trade;');
	header_close.insert(close_img);
	
	header.insert(header_text);
	header.insert(header_close);
	
	//content.insert(AC_FL_PreloadContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0','width','560','height','340','movie','http://www.youtube.com/v/BvqARv1nPOw&hl=en_US&fs=1&rel=0','quality','high','allowfullscreen','false' ));	/*content.insert('coming soon');*/
	container.insert(header);	container.insert(content);
	dbody.insert(background);
	dbody.insert(container);
	
	background.observe('click',hideVideo);
	close_img.observe('click',hideVideo);
	
	$('iVid_bg').show();
	$('iVid_cont').show();
	
	//iVid_content
	//var vid = new YT_Inr({video_id:'BvqARv1nPOw',player_id:'26ELpS3Wc4Q',player:{width:187,height:105,ratio:true},parent_id:'iVid_content',cover:false});
	//var vid = new YT_Inr({video_id:'ssnLsB2Z4-I',player_id:'26ELpS3Wc4Q',player:{width:640,height:364,ratio:true},parent_id:'iVid_content',cover:true});
	var vid = new YT_Inr({video_id:'eNmSMFVhkSE',player_id:'26ELpS3Wc4Q',player:{width:640,height:364,ratio:true},parent_id:'iVid_content',cover:{active:true,height:42,topPadding:false,title:'Get the Edge'},color:'black'});
	vid.show();
	if(vid.error)alert(vid.error_message);
}

function hideVideo() 
{	
	$('iVid_bg').hide();
	$('iVid_cont').hide();
	
	$('iVid_bg').remove();
	$('iVid_cont').remove();
}

function openVideoDiv() {
	if($('brands'))
		$('brands').innerHTML = AC_FL_PreloadContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0','width','740','height','39','movie','images/flash/topflash','quality','high','allowfullscreen','false','wmode','transparent' );
	if($('brands2'))
		$('brands2').innerHTML = AC_FL_PreloadContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0','width','655','height','39','movie','images/flash/bottomflash','quality','high','allowfullscreen','false','wmode','transparent' );
		
}

function get_document_window_height()
{
   if (document.body && document.body.clientHeight &&
       (document.body.clientHeight > 1)) return document.body.clientHeight;
   else if (window && window.innerHeight) return window.innerHeight;
   else if (document.documentElement && document.documentElement.offsetHeight)
      return document.documentElement.offsetHeight;
   else if (document.body && document.body.offsetHeight)
      return document.body.offsetHeight;
   return 0;
}

function get_document_window_width()
{
   if (document.body && document.body.clientWidth)
      return document.body.clientWidth;
   else if (window && window.innerWidth) return window.innerWidth;
   else if (document.documentElement && document.documentElement.offsetWidth)
      return document.documentElement.offsetWidth;
   else if (document.body && document.body.offsetWidth)
      return document.body.offsetWidth;
   return 0;
}

function convert_width(width,window_width)
{
   if (width == null) width = page_width + 52;
   else if (typeof(width) == 'string') {
      var percent_pos = width.indexOf('%');
      if (percent_pos != -1)
         width = window_width * (width.substr(0,percent_pos) / 100);
   }
   if (! document.all) {
      if (width > (window_width - 25)) width = window_width - 25;
   }
   else if (width > (window_width - 10)) width = window_width - 10;
   return width;
}

function convert_height(height,window_height)
{
   if (height == null) height = window_height;
   else if (typeof(height) == 'string') {
      var percent_pos = height.indexOf('%');
      if (percent_pos != -1)
         height = window_height * (height.substr(0,percent_pos) / 100);
   }
//   else height += header_offset;
   if (height > (window_height - 40)) height = window_height - 40;
   return height;
}

function calculate_left(width,window_width)
{
   var left = (window_width - width) / 2;
   if (! document.all) left += 10;
   if (left < 0) left = 0;
   return left;
}

function calculate_top(height,window_height)
{
   var top = ((window_height - height) / 2) - 3;
   if (top < 0) top = 0;
   return top;
}
function bookmarksite(title, url)
{
    if (document.all) window.external.AddFavorite(url, title);
    else if (window.sidebar) window.sidebar.addPanel(title, url, "");
}

window.onresize = function() {
   resize_bg()
}

window.onload = function() {
	openVideoDiv();
}
