$(document).ready(function(){
  // Up left menu
  var url = document.location.toString();
  spl_pos = url.indexOf('serial');
  if (spl_pos == -1){
    var cookies = get_cookies_array();
  	for(var name in cookies){
  	  coc = name.substr(0,11);
  	  if (coc == 'submenuMark') $.cookie(name, null, {expires: null, path: '/'});
  	}
	}

  // Left menu
  $('ul#my-menu ul').each(function(i) { // Check each submenu:
		if ($.cookie('submenuMark-' + i)) {  // If index of submenu is marked in cookies:
			$(this).show().prev().removeClass('collapsed').addClass('expanded'); // Show it (add apropriate classes)
		}else {
			$(this).hide().prev().removeClass('expanded').addClass('collapsed'); // Hide it
		}
		$(this).prev().addClass('collapsible').click(function() { // Attach an event listener
			var this_i = $('ul#my-menu ul').index($(this).next()); // The index of the submenu of the clicked link
			if ($(this).next().css('display') == 'none') {
			  // When opening one submenu, we hide all same level submenus:
			  $(this).parent('li').parent('ul').find('ul').each(function(j) {
					if (j != this_i) {
						$(this).slideUp(200, function () {
							$(this).prev().removeClass('expanded').addClass('collapsed');
							cookieDel($('ul#my-menu ul').index($(this)));
						});
					}
				});
				// :end
				$(this).next().slideDown(200, function () { // Show submenu:
					$(this).prev().removeClass('collapsed').addClass('expanded');
					cookieSet(this_i);
					color = $(this).prev().css("color");
					location.href = $(this).prev().attr('href');
				});
			} else {
			  if ($(this).find('a[href='+url+']').attr('href') != document.location){
				$(this).next().slideUp(200, function () { // Hide submenu:
  			  $(this).prev().removeClass('expanded').addClass('collapsed');
				  cookieDel(this_i);
				  //location.href = $(this).prev().attr('href');
					$(this).find('ul').each(function() {
						$(this).hide(0, cookieDel($('ul#my-menu ul').index($(this)))).prev().removeClass('expanded').addClass('collapsed');
					});
				});
			  }
			}
		return false; // Prohibit the browser to follow the link address
		});
	});
	
	//Selected menu item
	/*
	IE='\v'=='v';
  if (IE){
    $('#menu_left div').removeClass();
  } else {
	  $('ul li ul li:first-child a div.top-right-menu').css('right','-8px');
  }
  */
	
	$('#menu_left a[href='+url+'] > div').css('color','#ffffff').css('background-color','#01A8CA');
	
	var apages = Array('category','serial','page');
	for(i=0;i<apages.length;i++){
	  spl_pos = url.indexOf(apages[i]);
	  if (spl_pos != -1 || url == 'http://test6.vp.com.ua/'){
  	  $('#site').css('background-image','none');
  	  break;
  	}
	}

	// Galary photo zoom
	if ($('a.customGal').length)
  $('a.customGal').zoomimage({
  	controlsTrigger: 'mouseover',
  	className: 'custom',
  	shadow: 40,
  	controls: false,
  	opacity: 1,
  	beforeZoomIn: function(boxID) {
  		$('#' + boxID)
  			.find('img')
  			.css('opacity', 0)
  			.animate(
  				{'opacity':1},
  				{ duration: 500, queue: false }
  			);
  	},
  	beforeZoomOut: function(boxID) {
  		$('#' + boxID)
  			.find('img')
  			.css('opacity', 1)
  			.animate(
  				{'opacity':0},
  				{ duration: 500, queue: false }
  			);
  	}
  });

  //Add to basket
  $(".basket_prod").click(function (event){
    var url = $(this).attr('href');
    $.get(url, function(data){
      $('#basket_count_top').html(data);
    });
    alert($('#addad_basket').html());
  });

  //Menu
  $(".menu").hover(
    function () {
      $(this).css("background-color","#00c3e9");
    },
    function () {
      $(this).css("background-color","#01a8ca");
    }
  );
});

//Add to bookmark
function bookmarkthis(title,url) {
  if (window.sidebar) { // Firefox
    window.sidebar.addPanel(title, url, "");
  } else if (document.all) { // IE
    window.external.AddFavorite(url, title);
  } else if (window.opera && window.print) { // Opera
    var elem = document.createElement('a');
    elem.setAttribute('href',url);
    elem.setAttribute('title',title);
    elem.setAttribute('rel','sidebar');
    elem.click();
  }
  
  //Selected rows
  //$('.rows_').keyup(function() {alert('fff');});
  
  //$('.rows_').bind('keyup', function() { alert('hi') } );
  
  
} 

function cookieSet(index) {
	$.cookie('submenuMark-' + index, 'opened', {expires: null, path: '/'}); // Set mark to cookie (submenu is shown):
}
function cookieDel(index) {
	$.cookie('submenuMark-' + index, null, {expires: null, path: '/'}); // Delete mark from cookie (submenu is hidden):
}

function get_cookies_array() {

  var cookies = { };

  if (document.cookie && document.cookie != '') {
      var split = document.cookie.split(';');
      for (var i = 0; i < split.length; i++) {
          var name_value = split[i].split("=");
          name_value[0] = name_value[0].replace(/^ /, '');
          cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
      }
  }

  return cookies;
}
