$(function(){
	
	$('.back').click(function(){
		history.back();
		return false;
	});
	
	$('a[href^="http://"]').each(function(){
		/*var href = $(this).attr('href');
		var vlink = true;
		
		if(strpos( href, ''))
			vlink = false;
		
		if(vlink)*/
			$(this).attr('target','_blank');
	});
	
	// ------------------------ ÊÍÎÏÊÀ ÊÓÏÈÒÜ ----------------------------
	$.preloadImg("/img/buy_btn_hover.gif","/img/bb_btn_hover.gif");
	
	$('.buy_btn').mousedown(function(){ $(this).addClass("buy_btn_hover")});
	$('.buy_btn').mouseup(function(){ $(this).removeClass("buy_btn_hover"); });
	$('.buy_btn').mouseout(function(){ $(this).removeClass("buy_btn_hover")});
	
	$('.bb_btn').mousedown(function(){ $(this).addClass("bb_btn_hover")});
	$('.bb_btn').mouseup(function(){ $(this).removeClass("bb_btn_hover"); });
	$('.bb_btn').mouseout(function(){ $(this).removeClass("bb_btn_hover")});
	
	$('.buy_btn,.bb_btn').click(function(){
		var id = $(this).find('input').val();
		toCart(id, 1);
	});	
	
	// -------------------------
	$('.um_cell').hover(
		function(){
			$(this).addClass("um_cell_cur");
		},
		function(){
			$(this).removeClass("um_cell_cur");
		}
	);
	
	$('.bm_cell').hover(
		function(){
			$(this).addClass("bm_cell_cur");
		},
		function(){
			$(this).removeClass("bm_cell_cur");
		}
	);
	
	// -------------------------
	/*$('.news_title').dropShadow({
		//left	: [öåëîå ÷èñëî] (ïî óìîë÷àíèþ = 4) 
		//top    	: [öåëîå ÷èñëî] (ïî óìîë÷àíèþ = 4) 
		//blur   	: [öåëîå ÷èñëî] (ïî óìîë÷àíèþ = 2)
		//opacity	: [äðîáíîå ÷èñëî] (ïî óìîë÷àíèþ = 0.5)
		//color  	: [ñòðîêà] (ïî óìîë÷àíèþ = "black")
		//swap   	: [ëîãè÷åñêîå çíà÷åíèå] (ïî óìîë÷àíèþ = false)
		left : -19,
		top : -4,
		blur : 0,
		opacity : 1,
		color : "#fff"
	});*/
	
	$('.img_news').each(function(){
		var img = $(this).find('img');
		var img_h = img.height();
		
		var title =  $(this).find('div');
		var title_h = title.height();
		
		title.css('margin',(img_h-title_h-5)+'px 0 0 20px');
	});
	
	//
	$('.highslide').click(function(){
		return hs.expand(this);	
	});	
	
	//
	if($('#other_images').size())
	{
		$('#other_images td:first').prepend('<div class="stroke"></div>');
		
		var gl_td = $('#gl_gi').parent('td');
		var td_w = gl_td.width();
		var td_h = gl_td.height();
		
		var ind = $('#ind');
		ind.css('margin-left',absPosition(gl_td.get(0)).x-td_w/2-ind.width()/2);
		ind.css('margin-top',absPosition(gl_td.get(0)).y-td_h/2-ind.height()/2);
		
		$('#other_images img').click(function(){
			$('.stroke').remove();
			
			$(this).parent('td').prepend('<div class="stroke"></div>');
			
			var src = $(this).attr('src');
			fname = end(src.split('/'));
			
			$('#gl_gi img').hide();
			$('#ind').show();
			
			$('#gl_gi a').attr('href','/img_good/'+fname);
			$('#gl_gi img').attr('src','/img_good/300x300/'+fname).load(function(){
				$('#ind').hide();
				$(this).fadeIn('slow');				
			});
		});
	}
	
	//
	var $cur_path = $('#cur_path');
	if($cur_path.size())
	{
		$('.podbor_btn').click(function(){
			var fpf = $('input[name="fpf"]').val();
			var fpt = $('input[name="fpt"]').val();
			var cur_path = $cur_path.val();
			
			if(cur_path=='/catalog/')
				location.href = '/catalog/&fpf='+fpf+'&fpt='+fpt;
			else
			{
				m1 = cur_path.match(/&fpf=([^&]+)/);
				if(strpos(cur_path,'&fpf='))
					cur_path = str_replace('&fpf='+(m1?m1[1]:''),'&fpf='+fpf,cur_path);
				else
					cur_path += '&fpf='+fpf;
				
				m2 = cur_path.match(/&fpt=([^&]+)/);
				if(strpos(cur_path,'&fpt='))
					cur_path = str_replace('&fpt='+(m2?m2[1]:''),'&fpt='+fpt,cur_path);
				else
					cur_path += '&fpt='+fpt;
				
				location.href = cur_path;
			}
		});
	}
		
	// -------------------------
	$(document).pngFix();
});

// {{{ str_replace
function str_replace(search, replace, subject) {
    // Replace all occurrences of the search string with the replacement string
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_str_replace/
    // +       version: 812.1017
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'

    var f = search, r = replace, s = subject;
    var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;

    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    };

    return sa ? s : s[0];
}// }}}

function RegSessionSort(url,filter)
{
	toajax('/inc/session_sort.php?'+filter+'&location='+url);
}

function toCart(id, kol)
{
	$.ajax({
		type: "GET",
		url: "/cart.php",
		data: "action=tocart&id="+id+"&kol="+(kol*1<1?1:kol),
		success: function(data){
			data = str_replace('<script>','',data);
			data = str_replace('</script>','',data);
			eval(data);
		}
	});
}
