var STAR_GREY = '/media/static/i/stargrey.png'
var STAR_GREEN = '/media/static/i/stargreen.png'
var STAR_RED = '/media/static/i/starred.png'

function goTo(url){
	document.location = url
}
//onmouseover="this.src='/media/static/i/stargreen.png'" onmouseout="this.src='/media/static/i/stargrey.png'" onclick="lightbox_add({{p.pk}});this.onmouseover=function(){};this.onmouseout=function(){};this.src='/media/static/i/starred.png'"

function show_float_box(float_box){
	float_box.show()
	if(float_box.attr('timeout_hide')){
		clearTimeout(float_box.attr('timeout_hide'))
		float_box.removeAttr('timeout_hide')
	}
}

function init_gcell(){
	$('.gcell').mouseout(function(el){
		var float_box = $(this).next()
		var star_el = float_box.find('.lightbox_star')
		//console.log('lev '+star_el.attr('src') )
		if(star_el.attr('src') == STAR_GREY){
			float_box.attr('timeout_hide',
				setTimeout(function(){
					float_box.hide()
				},200))
		}
	}).mousemove(function(el){
		var float_box = $(this).next()
		show_float_box(float_box)
	})
	$('.unmarked_star').mousemove(function(el){
		if($(this).attr('src') != STAR_GREY){return}
		$(this).parent('.float_box').show()
		$(this).attr('src',STAR_GREEN)
	}).mouseleave(function(el){
		if($(this).attr('src') != STAR_GREEN){return}
		$(this).attr('src',STAR_GREY)
	}).click(function(el){
		if($(this).attr('src') == STAR_RED){return}
		$(this).attr('src',STAR_RED)
		var picture_id = $(this).parent('.float_box').prev().find('.picture_id').html()
		lightbox_add(picture_id)
	})
	$('.to_cart').mousemove(function(el){
		//$(this).parent('.float_box').show()
		show_float_box($(this).parent('.float_box'))
	})
    $('.float_box').mousemove(function(el){
		show_float_box($(this))
		///console.log('fb move')
		//$(this).show()
	})
	
	$('.lightbox_star').bt('Добавить в избранное',{
  width: 80, 
  fill: 'white', 
  cornerRadius: 20, 
  padding: 20,
  strokeWidth: 1
})

$('.to_cart').bt('Купить репродукцию',{
  width: 80, 
  fill: 'white', 
  cornerRadius: 20, 
  padding: 20,
  strokeWidth: 1
})
$('a.inner_zoom').fancybox()
	
	//$('.gcell')
}

function init_countes_events(){
	$('#lightbox_count, #lightbox_count_star').click(function(){
		goTo('/catalog/lightboxshow/')
	})
	$('#cart_count, #cart_count_star').click(function(){
		goTo('/customizer/order/')
	})
	
}

function hideblock(id){
  block=$("#"+id)
  dp=block.css('display')
  dp=(dp=='none'?'':'none')
  block.css('display',dp)
}

function set_lightbox_count(cc){
	if(!cc || cc == '' || cc== '0'){
		$('#lightbox_count').html('')
	}else{
		$('#lightbox_count').html(cc)
	}
}
                
function lightbox_add(id){
  $.get("/catalog/lightboxadd/"+id,{},function(data){set_lightbox_count(data+'')});
  
}

function lightbox_rem(el,id){
  $.get("/catalog/lightboxrem/"+id,{},function(data,textStatus){set_lightbox_count(data)});
  $(el).parent('.float_box').fadeOut('slow').prev().fadeOut('slow');
}

function get_cat_page(mode,parent){
            switch_next_prev(mode,'prevCatPage','nextCatPage')
            $.get("/catalog/getcatpage/?page="+cur_page+'&parent='+parent,'',function(data,textStatus){
		if (data==0) $("#cat_block_container").toggle()
                  else $("#catblock").html(data)
            })
}

function get_pcat_page(mode,painter){
            switch_next_prev(mode,'prevCatPage','nextCatPage')
            $.get("/catalog/getpcatpage/?page="+cur_page+'&painter='+painter,'',function(data,textStatus){
		if (data==0) $("#cat_block_container").toggle()
                  else $("#catblock").html(data)
            })
}

function get_painter_cat_page(mode){
            switch_next_prev(mode,'prevPainterCatPage','nextPainterCatPage')
            $.get("/catalog/getpcatpage/?page="+cur_page,'',function(data,textStatus){
		if (data==0) $("#pcat_block_container").toggle()
                  else $("#pcatblock").html(data)
            })
}

function switch_next_prev(mode,prevName,nextName){
    if(mode=='prev' && cur_page>1){
	
	if(cur_page==max_pages)	$("#"+nextName).css('visibility','')
	cur_page-=1
	if(cur_page==1)	$("#"+prevName).css('visibility','hidden')
	
    }else if(mode=='next' && cur_page<max_pages){
	
	if(cur_page==1)	$("#"+prevName).css('visibility','')
	cur_page+=1
	if(cur_page==max_pages)	$("#"+nextName).css('visibility','hidden')
	
    }else{
	
	if(cur_page==1)	$("#"+prevName).css('visibility','hidden')
	if(cur_page==max_pages)	$("#"+nextName).css('visibility','hidden')
	
    }
}

function get_painter_page(mode,namelike,style,country,city){
    switch_next_prev(mode,'prevPage','nextPage')
    url="/catalog/getpainterpage/?page="+cur_page+'&namelike='+namelike+'&country='+country+'&city='+city+'&style='+style
    $.get(url,'',function(data,textStatus){
	if(data=='0') $("#painter_block_container").toggle()
	else $("#painterblock").html(data)}
	)
}

function get_style_page(mode,parent){
      if(mode=='prev' && cur_style_page>1){
	
	if(cur_style_page==cur_style_page)	$("#nexts").css('visibility','')
	cur_style_page-=1
	if(cur_style_page==1)	$("#prevs").css('visibility','hidden')
	
    }else if(mode=='next' && cur_style_page<max_style_pages){
	
	if(cur_style_page==1)	$("#prevs").css('visibility','')
	cur_page+=1
	if(cur_style_page==cur_style_page)	$("#nexts").css('visibility','hidden')
	
    }else{
	
	if(cur_style_page==1)	$("#prevs").css('visibility','hidden')
	if(cur_style_page==max_style_pages)	$("#nexts").css('visibility','hidden')
	
    }
    $.get("/catalog/getstylepage/?page="+cur_style_page+'&parent='+parent,'',function(data,textStatus){
	if(data=='0') $("#style_block_container").toggle()
	else $("#styleblock").html(data)}
	)
}

function switch_category_block(mode){
      $('#hide').toggle()
      $('#show').toggle()
      $('#catblockover').toggle()
      $.cookie("category_block",mode)
}

function highlight_menu(){
	var cur_path = $.url.attr('path')
	if(cur_path.substring(0,9)!='/catalog/'){
		return
	}
	var cur_path = cur_path.substring(9).split('/')[0]
	$('#'+cur_path).addClass('selected_menu')
}
      
function switch_bio_block(mode){
      $('#hidebio').toggle()
      $('#showbio').toggle()
      $('#bioblock').toggle()
      $.cookie("bioblock",mode)
}

