$(document).ready(function(){
	
	/*Product description*/
	var reg=/#id=(\d*)/;
	if (arr=reg.exec(location.hash)) 
		window.location = (url_product_show + '/id/' + arr[1]); 
	$('#product-list ul a').click(function(){
		var prod_url = $(this).attr('href');
		$('#product-list ul a').removeClass('sel');
		$(this).addClass('sel');
		$('#prod-main-img').addClass('loading');
		$.ajax({
		   type: "POST",
		   dataType: "json",
		   url: prod_url,
		   success: function(msg){
			$('#prod-main-img a').attr('href', ('/uploads/product/' + msg.foto));
			$('#prod-main-img img').attr('src', ('/images/thumbs/timthumb.php?src=/uploads/product/' + msg.foto));
			$('#prod-main-img a').attr('title', msg.name);
			$('#prod-main-img img').load(function(){$('#prod-main-img').removeClass('loading');});
			$('#prod-text-desc p:eq(0) b').html(msg.name);
			$('#prod-text-desc p:eq(1) span').text(msg.size);
			$('#prod-text-desc p:eq(2) span').text(msg.composition);
			$('#prod-text-desc p:eq(3) span').text(msg.price);
			$('#prod-text-desc p:eq(4) span').text(msg.code_number);
			$('#prod-text-desc p:eq(5) span').text(msg.bar_code);
			
			location.hash = "id="+msg.id;
			document.title = msg.name + ' - МИШУТКА';
			
			$('#prod-related').html('');
			$.each(msg.relateds, function(i, v){
				if (i == 0)	$('#prod-related').html('<h3>Сопутствующие товары</h3><ul></ul>');
				$('#prod-related ul').html($('#prod-related ul').html() + '<li><a href="#"><img src="#"></a><div class="prod-related-desc"><p></p><p>Цена:<span></span></p></div></li>');
				$('#prod-related ul a:eq('+(i)+')').attr('href', (url_product_show + '/' + v.cat + '/' + v.id));
				$('#prod-related ul a:eq('+(i)+') img').attr('src', ('/images/thumbs/timthumb.php?src=/uploads/product/' + v.foto));
				$('#prod-related ul .prod-related-desc:eq('+(i)+') p:eq(0)').text(v.name);
				$('#prod-related ul .prod-related-desc:eq('+(i)+') p:eq(1) span').text(' ' + v.price + ' грн');
			});
		   }
		});
		return false;
	});
	/*-------------------*/
	

	/*Filter form*/
	$('#select_tag select').css({'opacity': 0});
	$('#select_tag select').css({'position': 'absolute'});
	$('#select_tag select').css({'z-index': -1});
	$('#select_tag').css({'position': 'relative'});
	var el = ''; 
	var new_select='';
	$('#select_tag select').each(function(s){
		var opt_size = $('option', this).size() + 1;
		var sel_id = $(this).attr('id');
		$('option', this).each(function(i){
			var val = $(this).attr('value');
			var text = $(this).html();
			if ($(this).attr('selected'))
				el = '<div name = "' + val + '" class="first_opt">' + text + '</div>' + el;		
			el += '<div name = "' + val + '" class="'+ sel_id +'">' + text + '</div>';
		});
		new_select += '<div onclick = "selectShow(this, \'' + sel_id + '\', true, true)" class="select_div s'+ s +'">' + el + '</div>';
		el='';
	});
	$('#select_tag').html($('#select_tag').html() + new_select);
	/*------------*/
	
	/*scrolls*/
    jQuery('#pane').jScrollPane({
    	scrollbarWidth:12, 
    	showArrows: true,
        dragMaxHeight:  44,
        dragMinHeight:  44,
        scrollbarMargin: 10,
        enableKeyboardNavigation: false
    });
    
    jQuery('#pane1').jScrollPane({
    	scrollbarWidth:12, 
    	showArrows: true,
        dragMaxHeight:  44,
        dragMinHeight:  44,
        enableKeyboardNavigation: false
    });
    /*--------*/
    
    
    /*Like Form*/
    $('#like_btn_voice').bind('click', function(){
    	$('#like-form').submit();
    	thanks();
    });
    
    function thanks()
    {
    	$('#like_btn_voice').html('Спасибо за Ваш голос');
    	$('#like_btn_voice').unbind('click');
    }
    
    if ($('#ilike_very').val() == '1')
    	$('#like_very').addClass('like-check_on');
    else if ($('#ilike_yes').val() == '1')	
    	$('#like_yes').addClass('like-check_on');
    else if ($('#ilike_dont').val() == '1')	
    	$('#like_dont').addClass('like-check_on');

    
    if ($('.like-check_on').length > 0) thanks();
    
    $('.like-check').click(function(){
    	$('#ilike_very, #ilike_yes, #ilike_dont').val('');
    	id = $(this).attr('id');
    	$('#i' + id).val('1');
    	//$('#like-form').submit();
    	$('#like_very, #like_yes, #like_dont').removeClass('like-check_on');
    	$(this).addClass('like-check_on');
    });
    
    $("#like-form").ajaxForm(function(){});
    $("#like-form select").css({opacity: 0});
    $("#like-form select").css({width: 0});
    $('#like-form').css({'position': 'relative'});
    $('#like_select_cont').html($('#like-form select option:selected').text());
    $('#like_select_up, #like_select_down').click(function(){
    	if ($('#like_select .like_address').length == 0)
    	{
	    	$('#like-form select option').each(function(i){
	    		$('#like_select_cont').before('<div class="like_address" name="'+$(this).attr('value')+'"><div>'+$(this).text()+'</div></div>');
	    		$('#like_select').css('height', (26 * (i + 1))+'px');
	    		$('#like_select').css('top', (-26 * (i + 1))+'px');
	    	});
    	}
    });
    $('#like_select .like_address').live('click', function(){
    	$('#like-form select').val($(this).attr('name'));
    	$('#like_select_cont').html($('#like-form select option:selected').text());
    	removeLikeAddress();
    });
    $('#like_select').live('mouseleave', function(){
    	removeLikeAddress();
    });
    function removeLikeAddress()
    {
    	$('#like_select .like_address').remove();
    	$('#like_select').css('height', (26)+'px');
		$('#like_select').css('top', (0)+'px');
    }
    /*---------*/
    
    /*Move images on main*/
    /*
    main_w = $('#images_main').width();
    $('#images_main div img').each(function(i){
    	var right_pos = i*(Math.floor(Math.random() * (80-50 + 1)) + 50);
    	$(this).css('position', 'absolute');
    	$(this).css('z-index', Math.floor(Math.random() * ($('#images_main div img').length - 1 + 1)) + 1);
    	if (i % 2 == 0)
    	{
    		$(this).css('left', right_pos + 'px');
    		$(this).animate({left: '+=' + (main_w - right_pos)}, Math.floor(((Math.floor(Math.random() * (60000 - 40000 + 1)) + 40000)/main_w*(main_w - right_pos))), 'linear', function(){left(this)});
    	}
    	else
    	{
    		$(this).css('right', right_pos + 'px');
    		$(this).animate({right: '+=' + (main_w - right_pos)}, Math.floor(((Math.floor(Math.random() * (60000 - 40000 + 1)) + 40000)/main_w*(main_w - right_pos))), 'linear', function(){right(this)});
    	}
    });
    function left(el)
    {
    	$(el).css('left', -$(el).width());
    	$(el).css('z-index', Math.floor(Math.random() * ($('#images_main div img').length - 1 + 1)) + 1);
    	$(el).animate({left: '+=' + (main_w + $(el).width())}, (Math.floor(Math.random() * (60000 - 40000 + 1)) + 40000) ,  'linear', function(){left(this);});
    }
    function right(el)
    {
    	$(el).css('right', -$(el).width());
    	$(el).css('z-index', Math.floor(Math.random() * ($('#images_main div img').length - 1 + 1)) + 1);
    	$(el).animate({right: '+=' + (main_w + $(el).width())}, (Math.floor(Math.random() * (60000 - 40000 + 1)) + 40000),  'linear', function(){right(this);});
    } */
    /*-------*/
	 
});

/*Move images on main*/
$(window).load(function(){
	if ($('#images_main div').length > 0)
	{
		main_w = $('#images_main').width();
		block_width = 0;
		margin_sum = 0;
		
		speed = 130000;
		slow_speed = 230000;
		$('#images_main div img').show();
        $('#images_main div img').each(function(i){
   			block_width = block_width + $(this).width() + parseInt($(this).css('marginLeft'));
   			margin_sum = margin_sum + parseInt($(this).css('marginLeft'));
        });
        $('#images_main div').width(block_width);

        $('#images_main div').css('left', (main_w-block_width+margin_sum) + 'px').addClass('slow');
        $('#images_main div:first').clone().appendTo('#images_main').css('left', (main_w-block_width*2+margin_sum*2) + 'px' );
        
        $('#images_main div:first').clone().appendTo('#images_main').css('left', 0 + 'px' ).removeClass('slow');
        $('#images_main div:first').clone().appendTo('#images_main').css('left', block_width + 'px' ).removeClass('slow');
        
        $('#images_main div:eq(3)').clone().appendTo('#images_main').css('left', (main_w-block_width) + 'px' );
        $('#images_main div:eq(3)').clone().appendTo('#images_main').css('left', (main_w-block_width*2) + 'px' );
        
        $('#images_main div').each(function(i){
        	if (i < 2)
        		$(this).animate({left: main_w}, slow_speed*(i+1), 'linear', function(){animateLeftSlow(this, slow_speed);});
        	else if (i < 4)
        		$(this).animate({left: -block_width}, speed*(i-1), 'linear', function(){animateRight(this, speed);});
        	else if (i < 6)
        		$(this).animate({left: main_w}, speed*(i-3), 'linear', function(){animateLeft(this, speed);});
        });
	}
	function animateLeft(el, speed)
	{
		$(el).css('left', (main_w-block_width*2) + 'px');
		$(el).animate({left: main_w}, speed*2, 'linear', function(){animateLeft(el, speed)});
	}
	function animateLeftSlow(el, speed)
	{
		$(el).css('left', (main_w-block_width*2+margin_sum*2) + 'px');
		$(el).animate({left: main_w}, speed*2, 'linear', function(){animateLeftSlow(el, speed)});
	}
	function animateRight(el, speed)
	{
		$(el).css('left', block_width + 'px');
		$(el).animate({left: -block_width}, speed*2, 'linear', function(){animateRight(el, speed)});
	}
});
/*------------*/

function selectShow(el, sel_id, show, is_submit)
{
	if (show) {
		opt_size = $('#'+sel_id + ' option').size() + 1;
		start_h = 26; //parseInt($(el).css('height'))
		$(el).css('height', (opt_size*start_h) + 'px');
		$(el).bind('mouseleave', function(){
			$('div', this).unbind('click');
			$(this).css('height', start_h + 'px');
			$(this).click(function(){selectShow(this, sel_id, true, is_submit);});
		});
		$('div[class!=first_opt]', el).bind('click', function(){
			el_class = ($(this).attr('class'));
			el_val = $(this).attr('name');
			$('select#'+el_class).val(el_val);
			$('select#'+el_class).parent().submit();
		});
		$(el).attr('onclick', '');
		$(el).click(function(){selectShow(el, sel_id, false, is_submit);});
	}
}
