var slides = [
	{img:'Costech Web Banners1 110511.jpg', url:'/contact-us.php'},
	{img:'Costech Web Banners2 110511.jpg', url:'/contact-us.php'},
	{img:'Costech Web Banners3 110511.jpg', url:'/products-services/zirconarch.php'},
	{img:'Costech Web Banners4 110511.jpg', url:'/products-services/zirconia-fc.php'},
	{img:'Costech Web Banners5 110511.jpg', url:'/products-services/bleaching-trays.php'},
	{img:'Costech Web Banners6 110511.jpg', url:'/products-services/elite-7.php'}
];
var slideIndex = 0;
var slideNextImg;
var slidePrevImg;
var testi = -1;

$(document).ready(function(){
	$('.ajax-wait-icon').ajaxSend(function (e, r, o) {
		$(this).show();
		$('.ajax-btn').attr('disabled', 'disabled');
	});

	$('.ajax-wait-icon').ajaxComplete(function (e, r, o) {
		$(this).fadeOut(1000);
		$('.ajax-btn').removeAttr('disabled');
	});
	
	slidePrevImg = $('#slidePrev');
	setTimeout(doSlider, 10000);
	$('#message-box').overlay({ mask: '#666666' });
	$('.rec-chart-btn').click(recommendationsChart);
	
	fadeTestimonial();
});

function doSlider(){
	slideIndex++;
	slideIndex %= slides.length;
	slideNextImg = $('<a href="#" id="slideNext"><img style="width:930px;height:310px;" alt="Banner" /></a>');
	$('img', slideNextImg).load(nextSlide);
	$('#bannerContent').append(slideNextImg);
	$('img', slideNextImg).attr('src', '/images/banner/' + slides[slideIndex].img);
	slideNextImg.attr('href', slides[slideIndex].url);
}

function nextSlide(){
	$('#bannerContent').animate({left: '-=930'}, 1500, slideDone);
}

function slideDone(){
	slidePrevImg.remove();
	$('#bannerContent').css('left', 0);
	slidePrevImg = slideNextImg;
	slidePrevImg.attr('id', 'slidePrev');
	setTimeout(doSlider, 10000);
}


function gotTestimonial(){
	setTimeout(nextTestimonial, 10000);
}

function fadeTestimonial(){
	var t = $('#page-header-testimonial p.testimonial');	
	var n = t.size();
	testi++;
	testi %= n;

	$(t[testi]).fadeIn(2000, gotTestimonial);
}


function nextTestimonial(){
	$('#page-header-testimonial p.testimonial:visible').fadeOut(2000, fadeTestimonial);
}

function callService(service, method, methodData, title, successMsg) {
	$.ajax({
		type: 'POST',
		url: '/services/' + service + '.asmx/' + method,
		data: $.toJSON(methodData),
		cache: false,
		dataType: 'json',
		contentType: "application/json; charset=utf-8",
		success: function (data) {
			var result = data.d;
			var err = 'An unknown problem has occurred during the operation.';
			if (undefined !== result.Redirect) {
				document.location.href = result.Redirect;
				return;
			}

			if (undefined !== result.Error) {
				err = result.Error;
			}

			if (undefined !== result.Success && undefined !== successMsg) {
				if (successMsg instanceof Function) {
					successMsg(data.d);
				}
				else {
				   messageBox('tick', title, successMsg);
				}
				return;
			}

			messageBox('error', title, err);
		},
		error: function (req, msg, ex) {
			messageBox('error', 'Operation Failed', 'An unknown problem has occurred during this operation.');
		}
	});

	return false;
}

function messageBox() {
	var box = $('#message-box');
	var subject = 'CosTech1';
	var html = '';
	var clss = '';
	var a = arguments.length;
	var b = 0;
	var hs = false;
	
	$('div:first', box).empty();
	$('h1.title1', box).attr('class', 'title1');
	if (3 == a) {
		clss = arguments[b++];
		a--;
	}

	if (2 == a) {
		subject = arguments[b++];
		a--;
		hs = true;
	}

	if (1 == a) {
		html = arguments[b++];
		a--;
	}

	$('h1.title1', box).attr('class', 'title1 ' + clss);
	$('h1.title1 span', box).text(subject);
	if (hs) {$('h1.title1', box).css('display', undefined);} else {$('h1.title1', box).css('display', 'none');}
	$('div:first', box).html(html);

	box.overlay().load();
	return false;
}

function recommendationsChart(){
	messageBox('<img src="/images/recommendations-chart.png" style="width:778px;height:551px" alt="Recommentations Chart" />');	
	return false;
}

