﻿if (typeof C2GHomeCover === "undefined") {
	C2GHomeCover = {}
} (function (a) {
	var cover_obj = null;
	var mainClassName = null;

	C2GHomeCover.setup = {
		init: function () {
			if (typeof DD_belatedPNG === "object") {
				DD_belatedPNG.fix(".png-fix")
			}
		}
	}, 
	C2GHomeCover.homecovers = {
		isFirstLoad:  true,
		init: function (loadFromFile, fileUrl, mainclass) {
			mainClassName = mainclass;
			cover_obj = a("." + mainClassName);

			if (loadFromFile) {
				var e = {
					url: fileUrl,
					type: "GET",
					dataType: "json",
					success: C2GHomeCover.homecovers.populate
				};
				a.ajax(e)
			} else {
				C2GHomeCover.homecovers.populate(coversDataObject)
			}
		},
		populate: function (j) {
//			for (i = 0; i < j.covers.length; i++) {
//				j.covers[i].loaded = false
//			}
			C2GHomeCover.homecovers.covers = j.covers;
			cover_obj.html(function (l) {
				var o = '<div class="cover">';
				o += '<div class="overlayWhite">';
				o += '<div class="waveCyan"></div>';
				o += '<div class="overlayWave">';
        		o += '<div class="contentBlock">';
				o += '<span class="txtLarge"></span>';
				o += '<span class="txtOrange"></span>';
				o += '<span class="txtCyan"></span>';
				o += '</div>';
				o += '<div class="blockLink"><a class="coverLink" href="#"></a></div>';
				o += '</div></div></div>';
				return o;
			});
			a("div.cover").duplicate(j.covers.length - 1).appendTo(cover_obj);

			for (i = 0; i < j.covers.length; i++) {
				C2GHomeCover.homecovers.display(i);
				j.covers[i].loaded = true;
			}

			C2GHomeCover.homecovers.animate();
		},
		display: function (index) {
			a("." + mainClassName + " div.cover").each(function (e) {
				if (e === index) {
					var b = C2GHomeCover.homecovers.covers[index];
					a(this).attr("style", "background-image:url(" + b.coverImageUrl + ");");
					a(this).attr("id", b.coverId);
					a(".waveCyan", this).hide();
					
					if(b.waveType == 'orange')
						a(".waveCyan", this).addClass("waveOrange");

					a(".txtLarge", this).html(b.title).hide();
					a(".txtOrange", this).html(b.subtitle).hide();
					a(".txtCyan", this).html(b.subsubtitle).hide();
					a(".coverLink", this).attr("href", b.navigateurl);
					a(".coverLink", this).attr("target", b.target);
					a(".coverLink", this).html(b.linkText);
				}
				//C2GHomeCover.homecovers.covers[e].loaded = true;
			});
		},
		animate: function () {
			$("." + mainClassName).cycle({ 
				fx: "c2gFade", 
				cleartype: 1,
				//speedIn: 2000,
				//speedOut: 1500,
				easeIn: "easein",
				easeOut: "easeout",
				startingSlide: 0,
				timeout: 8000,
				pause: 1
			});

			a("." + mainClassName).hover(function () {
				a("." + mainClassName).cycle("pause");
			}, function () {
				a("." + mainClassName).cycle("resume");
			});
		}
	}, 
	C2GHomeCover.app = {
		start: function (loadFromFile, fileUrl, mainclass) {
			C2GHomeCover.setup.init();
			C2GHomeCover.homecovers.init(loadFromFile, fileUrl, mainclass);
		}
	}
})(jQuery);

jQuery.fn.duplicate = function (d, c) {
	var b = [];
	for (var a = 0; a < d; a++) {
		$.merge(b, this.clone(c).get())
	}
	return this.pushStack(b)
};

$.fn.cycle.transitions.c2gFade = function ($cont, $slides, opts) {
	if(C2GHomeCover.homecovers.isFirstLoad)
	{
		C2GHomeCover.homecovers.isFirstLoad = false;
		if(jQuery.browser.msie)
		{
			$($slides[opts.currSlide]).find(".txtLarge").show(0, function () {
				setTimeout(function() {$($slides[opts.currSlide]).find(".txtOrange").show(0, function () {
					setTimeout(function() {$($slides[opts.currSlide]).find(".txtCyan").show(0, function () {
						setTimeout(function() {$($slides[opts.currSlide]).find(".waveCyan").animate({ width: 'show' });}, 1000);
					})}, 1000);
				})}, 1000);
			});
		}
	}

	if(jQuery.browser.msie)
	{
		for (i = 0; i < C2GHomeCover.homecovers.covers.length; i++)
			$($slides[i]).find(".waveCyan").hide();
	}

	opts.before.push(function (curr, next, opts) {
		$.fn.cycle.commonReset(curr, next, opts);
		opts.cssBefore.opacity = 0;
	});

	opts.after.push(function (curr, next, opts) {
		$(curr).find(".waveCyan").hide();
		$(curr).find(".txtLarge").hide();
		$(curr).find(".txtOrange").hide();
		$(curr).find(".txtCyan").hide();

		$(next).find(".txtLarge").show(0, function () {
			setTimeout(function() {$(next).find(".txtOrange").show(0, function () {
				setTimeout(function() {$(next).find(".txtCyan").show(0, function () {
					setTimeout(function() {$(next).find(".waveCyan").animate({ width: 'show' });}, 1000);
				})}, 1000);
			})}, 1000);
		});
	});

	$slides.not(':eq(' + opts.currSlide + ')').css('opacity', 0);
	opts.animIn = { opacity: 1 };
	opts.animOut = { opacity: 0 };
	opts.cssBefore = { top: 0, left: 0 };
};


