﻿var count = 0;
var int1;
var i = 0;
var k = 2;
var t = 'r';
var cont_width = '540px';
$(document).ready( function(){

/* do nothing on current links */
// $('a.current').click(function () {
// return false;
// });
/**/
	$(".fancybox").fancybox({
		hideOnOverlayClick : true, 
		showCloseButton:false, 
		transitionIn	: "elastic", 
		transitionOut : "elastic", 
		"speedIn" : 600, 
		"speedOut" : 200, 
		hideOnContentClick : true
	});

	$('#buttonSearch').click(function(){
		$('.search form:eq(0)').submit();
		return false;
	});

/* поиск */	

	$('#input_search').focus(function(){
		$(this).val('');
	}).blur(function(){
		if($(this).val() == '') $(this).val('Поиск');
	});

/* // поиск */


/* main menu hover */	
$('.menu td').mouseover(function(){
     	$(this).addClass('hover');
    }).mouseout(function(){
     	$(this).removeClass('hover');
    });
/* // main menu hover */	


/* submenu hover */	
	$('.submenu li').mouseover(function(){
     	$(this).addClass('hover');
    }).mouseout(function(){
     	$(this).removeClass('hover');
    });
count = jQuery('#slider-list td').length;
$('#slider-list').css('width', count*201+'px');

$( "#slider_line" ).slider({slide: function(event, ui) {
	var sl = $('#slider-list');
	sl.css('left', -ui.value*0.01*parseInt((count-6)*201));	
	}
});

if(count>6){
	int1 = self.setTimeout(function(){animate()},50);
	// int1 = self.setInterval("animate()",50);
 
$('#slider-list a, .slider_line a').hover(function(){
	// clearInterval(int1);
	clearTimeout(int1);
}, function(){
	// int1 = self.setInterval("animate()",50);
	int1 = self.setTimeout(function(){animate()},50);
});
}

/* // submenu hover */
	$('.video').append('<span class="play"/>');

	$('.video').click(function() {
		var href = this.href;
		$.fancybox({
			'autoScale' : false,
			'transitionIn' : 'none',
			'transitionOut' : 'none',
			'width' : 640,
			'height' : 480,
			'href' : '/i/VideoPlayer.swf',
			'type' : 'swf',
			'swf' : {
		   		'wmode' : 'transparent',
		   		'allowfullscreen': 'true',
		   		'flashvars' : 'url='+href
			}		
		});
		return false;
	});
	
	//if($('#my-dropdown, #my-dropdown2').length != 0)
	//	$('#my-dropdown, #my-dropdown2').sSelect();
	
	$('#codeRefresh').click(function(){
		$('')
		return false;
	});
	
	//$('#my-dropdown').change(function(){
	//	load_object();
	//});
	
	$('#addFormComments').submit(function(){
		$.ajax({
			url : window.location.href+'/add_comment/',
			type : 'POST',
			dataType : 'json',
			data : $(this).serialize() ,
			success : function(data){
				if(data.err){
					var t = 'Исправьте ошибки:<br>';
					if(data.name) t += data.name+'<br>';
					if(data.message) t += data.message+'<br>';
					if(data.code) t += data.code+'<br>';
					$.fancybox({
						overlayShow : false,
						content : '<div class="msg error">'+t+'</div>',
						showCloseButton : false
					});
					setTimeout(function(){
						$.fancybox.close();
					}, 2500);
				}else if(data.success){
					$('#addFormContComments input:text, #addFormContComments textarea').val('');
					$.fancybox({
						overlayShow : false,
						content : '<div class="msg success">'+data.success+'</div>',
						showCloseButton : false
					});
					setTimeout(function(){
						$.fancybox.close();
					}, 2500);
				}
			}
		});
		return false;
	});
	
		$('.img-photo-sm-box a').click(function(){
			var id = $(this).prev().val();
			$.fancybox.showActivity();
			$.get('./', {'info':id}, function(data){
				$('#userInfo').html(data).show();
				$.fancybox.hideActivity();
			});
			return false;
		});
	if($('#mycarousel').html()){
		cont_width = (parseInt(document.getElementById('objScroll').clientWidth)-130)+'px';
		$('#mycarousel').show();
		$('#mycarousel').jcarousel({
			initCallback: gallery_initCallback,
			scroll:1,
			animation: 2000,
			buttonNextEvent : false	
		});
		
		$('.img-photo-sm-box a').hover(
			function () {
				$(this).parent().addClass("user-selected");
				onephoto = $(this).parents('li:first');
				onephoto.find(".img-photo-sm-name").css('display', 'inline-block');
				onephoto.siblings().find(".img-photo-sm-name").hide();
			}, 
			function () {
				$(this).parent().removeClass("user-selected");
				$(this).parents('li:first').find(".img-photo-sm-name").hide();
			}
		);
		$(window).resize(function() {
			if($('.jcarousel-container-horizontal, .jcarousel-clip-horizontal').html()){	
				$('.jcarousel-container-horizontal, .jcarousel-clip-horizontal').css('width','10px');			
				$('.jcarousel-container-horizontal, .jcarousel-clip-horizontal').css('width',(parseInt($('#pageCont').innerWidth())-130)+'px');
			}
		});
	}
});


var scroll = 0;
function gallery_initCallback(carousel) {
	var timern
	var intervaln = 1;
	// Вперёд	
     $(".jcarousel-next").bind('mouseover', function(){
    	 timern = setInterval(function(){carousel.next()},intervaln); 
      });
     $(".jcarousel-next").bind('mouseout', function(){
    	 clearInterval(timern);
      });
	      

	// Назад
    $(".jcarousel-prev").bind('mouseover', function(){
		timern = setInterval(function(){carousel.prev()},intervaln);        
	});
    $(".jcarousel-prev").bind('mouseout', function(){
		clearInterval(timern);       
	});
	$('.jcarousel-container-horizontal, .jcarousel-clip-horizontal').width(cont_width);    
}


function animate(){
	var set;
	var s = $("#slider_line a:first");
	if(i <= 0 && t == 'l'){
		k = 2;
		t = 'r';
	}
	if(i >= 100 && t == 'r'){ 
		k = -2;
		t = 'l';
	}
	var s_pr = k*100/parseFloat($('#slider-list').outerWidth());
	i = parseFloat(s.css('left'))+s_pr;
	s.css('left', i+'%');
	var sl = $('#slider-list');		
	sl.css('left', -parseFloat($("#slider_line a:first").css('left'))/100*parseInt((count-6)*201));
	int1 = self.setTimeout(function(){animate()},10);
}


function load_object(){
	$.fancybox.showActivity();
	$('#listQuestion').load(window.location.href, {'list_question' : $('#my-dropdown').val()}, function(){
		$.fancybox.hideActivity();
		$('.p_title-reply a').click(function() {
			reply = jQuery(this).parents('div:first');
			reply.find(".reply-box").slideToggle("normal");
			$('#addFormContComments').hide();
			$('#listQuestion').after($('#addFormContComments'));
			reply.find(".p_comment-add a").removeClass('show-form');
			reply.siblings().find(".reply-box").hide();
			return false;
		});
		$('.p_comment-add a').click(function(){
			if($(this).hasClass('show-form')){
				$('#addFormContComments').slideToggle('normal', function(){
					$('#faqParentId').val('');
				});
				$(this).removeClass('show-form');
			}else{
				$(this).addClass('show-form');
				$(this).parent().after($('#addFormContComments'));
				$('#faqParentId').val($(this).next().val());
				$.getJSON(window.location.href, {'code':'refresh'}, function(data){
					$('#codeImg').attr('src', data.src);
					$('#codeHash').val(data.hash);
					$('#addFormContComments').slideToggle('normal');
				});
			}
			return false;
		});
		
		$(".reply-box .all_comments .one-comment:first-child").addClass("first-comment");
		$('.p_comment-title a').click(function() {
			$(this).parents('div:first').find(".all_comments").slideToggle("slow");
			return false;
		});
	});	
}

function isDigit(data){
	numStr="0123456789()- +";
	k = 0;
	for (i=0;i<data.length;i++){
		thisChar = data.substring(i, i+1);
		if (numStr.indexOf(thisChar) != -1) k++;
	}
	if (k == data.length) return 1
	else return 0
}

