$(document).ready(function () {

	// Scrollable partners
	$.easing.backout = function(x, t, b, c, d){
		var s=1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	};

	$('#slideshow').serialScroll({
		itemWidth:106,
		items:'li',
		prev:'.scrollable a.prev',
		next:'.scrollable a.next',
		offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
		start:0, //as we are centering it, start at the 2nd
		duration:1000,
		force:true,
		stop:true,
		lock:false,
		cycle:true,
		easing:'backout', //use this easing equation for a funny effect
		interval: 5000
	});

	var scrollableWrapperWidth = $('#slideshow ul li').length * 106 + 'px';
	$('#slideshow ul').css('width', scrollableWrapperWidth);


	// Scrollable effect for thumbnails in catalog detail view
	$('#catalog_slideshow').serialScroll({
		itemWidth:72,
		step: 1,
		items:'li',
		prev:'.catalog-detail a.prev',
		next:'.catalog-detail a.next',
		offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
		start:0, //as we are centering it, start at the 2nd
		duration:300,
                force:false,
		stop:true,
		lock:false,
		cycle:true,
		easing:'linear', //use this easing equation for a funny effect
		jump: true


	});



	var catalogScrollableWrapperWidth = $('#catalog_slideshow ul li').length * (72 + 7) + 'px';
	$('#catalog_slideshow ul').css('width', catalogScrollableWrapperWidth);



	// Catalog list mouse over easing
	$('ul.catalog-list li').bind('mouseenter', function() {
		$(this).find('img.secondimage').fadeTo(200, 1);
		$(this).find('img.firstimage').fadeTo(200, 0);
		$(this).find('.label').fadeTo(200, 1);
	});

	$('ul.catalog-list li').bind('mouseleave', function() {
		$(this).find('img.secondimage').fadeTo(200, 0);
		$(this).find('img.firstimage').fadeTo(200, 1);
		$(this).find('.label').fadeTo(200, 0);

	});




	// Custom scrolling of right part
	var stairwayHeight = 940;
	var elements = document.getElementsByTagName('html');
	htmlElement = elements[0];

	$(document).bind('scroll', function(e) {
		return;
		//if ($(window).height() > stairwayHeight) {
			if (navigator.userAgent.indexOf('Chrome') != -1) {
				documentScrollTop = document.body.scrollTop;
			} else {
				documentScrollTop = htmlElement.scrollTop;
			}
			var containerTop = parseInt(($(window).height() - stairwayHeight) * documentScrollTop / ($(document).height() - $(window).height())) + 'px';

			$('td#main_right .container').css('top', containerTop);
		//}
	});




// Jquery UI widgets
	// Jquery UI dialog - Callback

        if(document.getElementById('pictureSlideshow') != null ){
                    $('#pictureSlideshow').dialog({
			autoOpen: false,
			width: 650,
                        dialogClass: 'hideTitle',
			buttons: {
				"Ok": function() {
					$(this).dialog("close");
				},
				"Cancel": function() {
					$(this).dialog("close");
				}
			}
		});
        }
	if (document.getElementById('callback_form') != null) {

		$('#callback_form').dialog({
			autoOpen: false,
			width: 600,
			buttons: {
				"Ok": function() {
					$(this).dialog("close");
				},
				"Cancel": function() {
					$(this).dialog("close");
				}
			}
		});


		// Callback form
		$('.callback-button').click(function(){
			$('#callback_form').dialog('open');
			$('#darken_background').css('display', 'block');
			return false;
		});



		$('.ui-dialog-titlebar-close').click(function(){
			$('#darken_background').css('display', 'none');
                         $('#successMessage').css('display', 'none');
                          $('#callback_form_inner').css('display', 'block');
		});

		// Time picker in callback form
		$('.time').timepicker({
			timeFormat: 'h:m',
		timeOnlyTitle: 'Âûáåðèòå âðåìÿ',
			timeText: 'Âðåìÿ',
			hourText: '×àñû',
			minuteText: 'Ìèíóòû',
			secondText: 'Ñåêóíäû',
			currentText: 'Ñåé÷àñ',
			closeText: 'Çàêðûòü'
		});
	}

        $('.content-image-to-wrap').wrap('<div class="content-image float-left"><div class="wrap"></div></div>');
        $('blockquote').wrap('<div class="quoted-text-2"><div class="wrap"></div></div>');

          $('#biggerImmage').attr('src', $('#catalog_slideshow img').first().attr('src_bigger'));

});


function windowScroll() {
	$('td#main_right .container').css('height', $(window).height() + 'px');
	$('td#main_right .container').css('top', htmlElement.scrollTop + 'px');
}



function strip_tags(html){

		//PROCESS STRING
		if(arguments.length < 3) {
			html=html.replace(/<\/?(?!\!)[^>]*>/gi, '');
		} else {
			var allowed = arguments[1];
			var specified = eval("["+arguments[2]+"]");
			if(allowed){
				var regex='</?(?!(' + specified.join('|') + '))\b[^>]*>';
				html=html.replace(new RegExp(regex, 'gi'), '');
			} else{
				var regex='</?(' + specified.join('|') + ')\b[^>]*>';
				html=html.replace(new RegExp(regex, 'gi'), '');
			}
		}

		//CHANGE NAME TO CLEAN JUST BECAUSE
		var clean_string = html;

		//RETURN THE CLEAN STRING
		return clean_string;
	}

/*
        $.fn.clearForm = function() {
  return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
      return $(':input',this).clearForm();
    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = '';
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    else if (tag == 'select')
      this.selectedIndex = -1;
  });
};
*/
function changePicture(element){
    $('#biggerImmage').attr('src', $(element).attr('src_bigger'));

}

function startShow(element){

    var currentIndex = 0;
    $('#catalog_slideshow img').each(function(i, e){
        if($(e).attr('src_bigger') == $(element).attr('src')){
            currentIndex = i;
        }
    })

           if(currentIndex==0){
            $('#pictureSlideshow .prev').css('display', 'none');
       }
       else{
            $('#pictureSlideshow .prev').css('display', '');
       }

       if(currentIndex== $('#catalog_slideshow img').length-1){
            $('#pictureSlideshow .next').css('display', 'none');
       }
       else{
            $('#pictureSlideshow .next').css('display', '');
       }

    var xl = $($('#catalog_slideshow img')[currentIndex]).attr('src_xl');
   $('#pictureSlideshow img').attr('src', xl);
   $('#darken_background').css('display', 'block');
   $('#pictureSlideshow').dialog('open');
}

function imageShowNext(){
   return  imageShowInner(1,  $('#catalog_slideshow img').length-1);

}

function imageShowPrev(){
  return   imageShowInner(-1, 0);
}
function imageShowInner(step, equal){

      $('#catalog_slideshow img').each(function(i, e){
        if($(e).attr('src_xl') == $('#pictureSlideshow img').attr('src')){
            currentIndex = i;
        }
    });

       if(currentIndex+step==0){
            $('#pictureSlideshow .prev').css('display', 'none');
       }
       else{
            $('#pictureSlideshow .prev').css('display', '');
       }

       if(currentIndex+step== $('#catalog_slideshow img').length-1){
            $('#pictureSlideshow .next').css('display', 'none');
       }
       else{
            $('#pictureSlideshow .next').css('display', '');
       }

    if(currentIndex ==  equal){
        return false;
    }

    var xl = $($('#catalog_slideshow img')[currentIndex+step]).attr('src_xl');
    $('#pictureSlideshow img').attr('src', xl);



    return false;
}
