$(document).ready(function(){

  var w = screen.width;
  if (w < 1200){
    $('#ent embed').height('107px').width('120px');
  }
  
  // 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);
					location.href = $(this).prev().attr('href');
				});
			}else {
			  if ($(this).child().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');
  }
  */
	
	var url = document.location.toString();
	var spl_pos = url.indexOf("/page/");
	
	if (spl_pos != -1){
	  url = url.substring(0,spl_pos);
	}
	
  $('#menu_left a[href='+url+'] > div').css('color','#ffffff').css('background-color','#58585a');
  
	// 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());
  });
  
  //Geay scale
  /*
  $(".enter_img").each(function(){
    grayscale($(this));
  });

  $(".enter_img").mouseover(function(){
    grayscale.reset($(this));
  }).mouseout(function(){
    grayscale($(this));
  });
  */
  var w = screen.width;
  switch(w){
    case 1024:{
      $('#active').css({'margin':'30px 0 0 25px'}); 
      break;
    }
    case 1152:{
      $('#active').css({'margin':'30px 0 0 90px'});
      break;
    }
    case 1280:{
      $('#active').css({'margin':'30px 0 0 150px'}); 
      break;
    }
    case 1680:{
      $('#active').css({'margin':'30px 0 0 360px'}); 
      break;
    }
    case 1920:{
      $('#active').css({'margin':'30px 0 0 470px'}); 
      break;
    }
  }
});

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 add_select(category,id){

  var root_url = $("#root_url").html();
  url1 = root_url+'get_cat_prop/cat/'+category;
  url2 = root_url+'add_select/cat/'+category;
  $.get(url1, function(data){
    $('#category_prop').html(data);
  });
  $.get(url2, function(data){
    $('#'+id).html(data);
  });
}
