var intval = null;

function nextSlide() {
    var active = $('#splash-control li.active');
    if( active.length == 0 ) var active = $('#splash-control li:last');
    var next = active.next().length ? active.next() : $('#splash-control li:first');
    active.removeClass();
    next.addClass('active');
    $('#spash-image').html($('#splash-content>'+next.children().attr('href')).html());
}

function triggerSlideShow(){
//	$('#widesplash').hide('slow');
//	$('.widesplash-trigger').remove();
	nextSlide();
	//intval = setInterval( 'nextSlide()', 5000 );
}



$(document).ready(function(){

	var fslide = $('#spash-image').html();
	$("#video-panel img, #overlay-trigger").click(function() { 
	    flowplayer('player', { 
	        src: '/js/flowplayer/flowplayer-3.1.2.swf',          
	        onFail: function() { 
	            document.getElementById("info").innerHTML = 
	                "You need the latest Flash version to view MP4 movies. " + 
	                "Your version is " + this.getVersion() 
	            ; 
	        }
	    }, { 
	        clip: '/videos/'+this.className+'.flv' 
	    });
		$(this).overlay({effect: 'apple'}); 
	});

    if($('#newsticker').length) $('#newsticker').newsTicker();
	setTimeout('triggerSlideShow()', 0 );
	var intval = setInterval( 'nextSlide()', 10000 );



	var pfh = $('#product-features').height();
	if(pfh>$('#product-description').height()) $('#product-description').height(pfh);
	
	$('#splash-control a').click(function() {
//		$('#widesplash').hide('slow');//@deprecated
//		$('.widesplash-trigger').remove();//@deprecated
		clearInterval(intval);
		$(this).parent().addClass('active').siblings().removeClass();
		$('#spash-image').html(
			$('#splash-content>'+$(this).attr('href')).html()
		);
	});
	
	$('#splash-title a').click(function() {
		clearInterval(intval);
		$('#spash-image').html(fslide);
		$('#splash-control li').removeClass();
	});
	
	$("div.scrollable").scrollable();
	
	$('img[rel]').overlay();
	
	$('.items div').click(function() {
		$('#products-wrapper>div').hide();
		$('#'+$(this).attr('title')).show();
	});
	
	var er = new Array();
	var count = 0;
	$('#fieldset-devid .errors, #fieldset-prepaid .errors').each(function(){
		var id = $(this).parent().parent().parent().attr('id');
		var val = $('>li',this).html();
		if(! er[id]) {
			er[id] = new Array();
			i=0;
			count++;
		}
		if($.inArray(val, er[id])==-1) {
			er[id][i] = $('>li',this).html();
			i++;
			if(i>1) {
				$(this).css('margin-top','-50px');
				if(count==1) $(this).css('margin-left','215px');
				if(count==2) $(this).css('margin-left','137px');
			}
		} else {
			$(this).remove();
		}
		
	});
	$('#poll #register').live('click',function(e) {		
		e.preventDefalut;
		var poll = $('#poll input:radio:checked').val();
		if(poll == undefined) {
			   $("#poll .error").text("Выберите пункт").show().fadeOut(3000);
			   return false;
	   } else {
			jQuery.ajax({
			  type: "POST",
			  data: 'poll=' + poll,
			  url: $('#poll form').attr('action'),
			  timeout: 45000,
			  error: function() {
			    alert('Попробуйте проголосовать позже. Идут технические работы.');
			  },
			  success: function(r) {
			    $('#poll').empty().html(r);
			  }
			});
			return true;
	   }
		
	});

	/**
     * FAQ behaviour
     */
    $('.faq_quest span').toggle(
		function() {
			$(this).parents('ul').addClass('faq_quest_open').find('.faq_answ').show('fast');
			$(this).parents('li:first').find('#open_ident').html('–');
		},
		function() {
			$(this).parents('ul').removeClass('faq_quest_open').find('.faq_answ').hide('fast');
			$(this).parents('li:first').find('#open_ident').html('+');
		}
	);

    var opened;

    /**
     * FAQ Admin behaviour
     */
	var editor;
    $('ul.faq_quest_admin li.faq-title').click(function(e){
		
		var form = $('.edit_faq');
		var obj = $(this);
		if (editor != undefined) {
			console.log("removing");
			CKEDITOR.remove(editor);
			$('#cke_textus').remove()
			editor = undefined;
		}
		form.fadeOut('fast', function(){
			$('li.form-place', obj.parent('ul')).append(form);
			$('input[name=faq_quest]', form).val($('span.title', obj).html());
			$('select[name=category_id]', form).val($('input.category_id', obj).val());
			$('input[name=enabled]', form).attr({checked: $('input.enabled', obj).val() == 1});
			$('input[name=faq_id]', form).val($('input.id', obj).val());
			$('input[name=order_index]', form).val($('input.order_index', obj).val());
			$('#textus').html($('ul.faq_answ_admin li:first', obj).html());
			editor = CKEDITOR.replace('textus');
			form.fadeIn('fast');
		});
		e.preventDefault();
		return false;
	});
    /**
     * MIPS download behaviour
     *
     */
    $('#mips_sup_models').toggle(
        function() {
            $('.mips-warning').show(150);
        },
        function() {
            $('.mips-warning').hide(150);
        }
    )
});

function SubmitAjax(form, callback){
	$.ajax({
		url: form.action,
		type: form.method,
		data: $(form).serialize(),
		success: callback
	});
	return false;
}
