var fieldvalue = {"search":"Product Search"};

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' : '');      
    }
};

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

var cssMenu = {
   menu_obj: {},
   current_id: null,
	init: function (element) {
			
		//Event.observe(document, 'mousemove', cssMenu.getcords);
		
		if($(element))
	   {
			var menu_array = $(element).childElements();
			
			cssMenu.create_menus(menu_array,"m1");	
		}
	},
	getcords: function(e){
				mouseX = Event.pointerX(e);
				mouseY = Event.pointerY(e);
						
				//$('debug2').innerHTML = cssMenu.current_id+' - mouseX:' + mouseX + '-- mouseY:' + mouseY;
				
				if(cssMenu.menu_obj[cssMenu.current_id])
				{
					m = cssMenu.menu_obj[cssMenu.current_id];
		
					mc = m.submenu.className;
					mc = mc.split("_");
					
					if(m.submenu.className == mc[0]+'_on')
					{
						//$('debug').innerHTML = m.submenu.className;
						
						offsets = Element.cumulativeOffset(m.submenu);
						mtop     = offsets[1]-30;
						mleft    = offsets[0];
						mbottom  = m.submenu.getHeight()+mtop+30;
						mright   = m.submenu.getWidth()+mleft;
						
						//$('debug2').innerHTML = mleft+"+"+m.submenu.getWidth()+"="+mright+"|"+mtop+"+"+m.submenu.getHeight()+"="+mbottom+"|"+'mouseX:' + mouseX + '-- mouseY:' + mouseY;
						if(!(mouseX > mleft && mouseX < mright) || !(mouseY > mtop && mouseY < mbottom))
						{
							m.submenu.removeClassName(mc[0]+'_on')
						   m.submenu.addClassName(mc[0]+'_off')
						}
					}
				}
	},
	create_menus: function(menu,parent) {
		menu.each(function(m)
		{
			var submenu_array = m.childElements();
			
			if(submenu_array.length > 1)
			{
				mid = m.id
				if(mid != ""  && !mid.include("ul"))
				{
			         cssMenu.create_menus(submenu_array,m.id);
						cssMenu.menu_obj[m.id] = {"submenu":submenu_array[1],"parent_menu":parent};
						m.observe('mouseover',cssMenu.showMenu);
						m.observe('mouseout',cssMenu.hideMenu);
				}
			}
		})
						//if($('debug2'))
						//$('debug2').innerHTML = Object.keys(cssMenu.menu_obj);
	},
	showMenu: function(e) {
		
		//$('debug2').innerHTML = cssMenu.menu_obj[this.id].submenu.id + " | ";
		
		//cssMenu.current_id = this.id;
		m = cssMenu.menu_obj[this.id].submenu;
		pm = cssMenu.menu_obj[this.id].parent_menu;
		mc = m.className;
		mc = mc.split("_");
		
		m.removeClassName(mc[0]+'_off');
		m.addClassName(mc[0]+'_on')
	},
	hideMenu: function(e) {
		
		m = cssMenu.menu_obj[this.id].submenu;
		
		mc = m.className;
		mc = mc.split("_");
		
		m.removeClassName(mc[0]+'_on')
		m.addClassName(mc[0]+'_off')
		
	}
};

function clearField(element)
{
	if(fieldvalue[element.name])
		if(fieldvalue[element.name] == element.value) {
		   element.value = "";
		}
		else if(element.value == "") {
		   element.value = fieldvalue[element.name];
		}
}

function switchView(key,value)
{
	Cookie.init({name: 'effectiveCCartlayout', path: '/'});
	
	if(key == 'layout')
		if(Cookie.getData('layout')==3)
			Cookie.setData('pagesize',6);
		else
			Cookie.setData('pagesize',6);
			
	Cookie.setData(key,value);
	
	curhref = window.location.href;
	
	newhref = curhref.split("/p-");
	//alert(newhref[0]);
	if(newhref.length > 1) {
		test = newhref[0].split("/artist-");
		if(test.length > 1) window.location.href = newhref[0]+"/";
		else window.location.href = newhref[0]+"/p-1/";
	}
	else window.location.reload();
}

function toggle_advanced() {
	if($('advanced_search').style.display == 'none')
	   Effect.SlideDown('advanced_search');
	else if($('advanced_search').style.display == '')
	   Effect.SlideUp('advanced_search');	
}

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 bookmarksite(title, url)
{
    if (document.all) window.external.AddFavorite(url, title);
    else if (window.sidebar) window.sidebar.addPanel(title, url, "");
}


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;
}
	
window.onload = function() {
	cssMenu.init('header_menu');
}