/*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true, strict: true */
/*global $: false, window: false */

"use strict";

window.SMS_CREATIVESHOWCASE = {
	sel: ".creativeshowcase",
	classpath: "/classlibrary/page/creativeshowcase/",
	init: function () {
		this.el = $(this.sel);
		
		this.getClassPath();
		
		this.makeSubmittable();
		
		this.makeLightbox();
		this.handleLightbox();
	},
	
// Get the Classpath the best way we know how
	getClassPath: function () {
		var classpath = $("script[src*=creativeshowcase]").attr("src");
		this.classpath = classpath.split("_scripts")[0];
	},
	
// Make the category drop-down auto-submitting
	makeSubmittable: function () {
		this.el
			.find("#cs_categories")
				.children(".button").hide().end()
				.children("#cs_category")
					.change(function () {
						$(this).parent("form").submit();
					});
	},
	
// Add lightbox to creative showcase items
	makeLightbox: function (prid, iid) {
		var that = this;

		this.el
			.find("a[href*=creativeshowcasedetail]")
				.click(function (ev) {
					var pageregionid = this.href.split("creativeshowcasedetail")[1].split(".")[0], itemid = this.href.split("itemid=")[1], hasbrowse = $(this).parents("#creativeshowcase_browse_" + pageregionid).length;
					
					ev.preventDefault();

					that.displayLightbox(pageregionid, itemid, 1, hasbrowse);
				});
	},
	handleLightbox: function () {
		var that = this, urlhash = { pageregionid: 0, itemid: 0, samplenum: 1 }, hasbrowse = 0;
		
		$(".creativeshowcase_lightbox")
			.live("click", function (ev) {
				if ($(ev.target).hasClass("creativeshowcase_lightbox")) {
					location.hash = "";
					$(this).remove();
				}
			})
		// Back button into "close" button
			.find(".back")
				.live("click", function (ev) {
					ev.preventDefault();
					
					location.hash = "";
					$(this).parents(".creativeshowcase_lightbox").remove();
				}).end()
		// Previous and Next update lightbox
			.find(".navigation_simple a")
				.live("click", function (ev) {
					var pageregionid = $(this).parents(".creativeshowcase_lightbox").attr("id").split("_")[2], itemid = this.href.split("=")[1], hasbrowse = $(this).parents("#creativeshowcase_browse_" + pageregionid).length;

					ev.preventDefault();
					
					that.displayLightbox(pageregionid, itemid, 1, hasbrowse);
				}).end()
		// Allow us to view individual samples
			.find(".navigation_individual a")
				.live("click", function (ev) {
					var pageregionid = $(this).parents(".creativeshowcase_lightbox").attr("id").split("_")[2], urlvars = this.href.split("?")[1].split("&"), urlvar = [], showcaseid = 0, samplenum = 0;
					
					ev.preventDefault();
					
					$.each(urlvars, function (i, v) {
						urlvar = v.split("=");
						
						switch (urlvar[0]) {
						case "itemid":
							showcaseid = urlvar[1];
							break;
						case "sample":
							samplenum = urlvar[1];
							break;
						}
					});
					
					$(this).parent().addClass("current").siblings().removeClass("current");
					
					that.displayLightboxSample(pageregionid, showcaseid, samplenum);
				});
				
		// What if we've bookmarked a sample?
		if (location.hash.length > 0) {
			urlhash.vars = location.hash.substring(1).split("&");
			
			$.each(urlhash.vars, function (i, v) {
				urlhash.thisvar = v.split("=");
				
				switch (urlhash.thisvar[0]) {
				case "pageregionid":
					urlhash.pageregionid = urlhash.thisvar[1];
					break;
				case "itemid":
					urlhash.itemid = urlhash.thisvar[1];
					break;
				case "samplenum":
					urlhash.samplenum = urlhash.thisvar[1];
					break;
				}
			});
			
			if (urlhash.pageregionid > 0 && urlhash.itemid > 0) {
				hasbrowse = $("#creativeshowcase_browse_" + urlhash.pageregionid).length;
				
				this.displayLightbox(urlhash.pageregionid, urlhash.itemid, urlhash.samplenum, hasbrowse);
			}
		}
	},
	displayLightbox: function (prid, iid, snum, iscs) {
		var that = this, pageregionid = prid || 0, itemid = iid || 0, samplenum = snum || 1, hasbrowse = iscs || 0, lightbox = $("#creativeshowcase_lightbox_" + pageregionid), junk = new Date().getTime();
		
		$.get(this.classpath + "public.cfc",
			{ "method": "remoteDetail", "returnFormat": "plain", "pageregionid": pageregionid, "itemid": itemid, "samplenum": samplenum, "hasbrowse": hasbrowse, "junk": junk },
				function (result, status) {
					if (status === "success" && result.length > 0) {
						if (lightbox.length === 0) {
							lightbox = $("<div />").attr("id", "creativeshowcase_lightbox_" + pageregionid).addClass("creativeshowcase_lightbox").appendTo($(that.sel).get(0));
						}
						
						lightbox
							.html(result)
							.children(":first")
								.css("margin-top", function () {
									return (lightbox.height() - lightbox.find(".showcase_sample").height()) / 2;
								});
						
						location.hash = "#pageregionid=" + pageregionid + "&itemid=" + itemid + "&samplenum=" + samplenum;
					}
				});
	},
	displayLightboxSample: function (prid, sid, snum) {
		var pageregionid = prid || 0, showcaseid = sid || 0, samplenum = snum || 0, lightbox = $("#creativeshowcase_lightbox_" + pageregionid), junk = new Date().getTime();
		
		$.get(this.classpath + "public.cfc",
			{ "method": "remoteDetailSample", "returnFormat": "plain", "showcaseid": showcaseid, "samplenum": samplenum, "junk": junk },
				function (result, status) {
					if (status === "success" && result.length > 0) {
						lightbox
							.children(".creativeshowcase_detail")
								.children(".showcase_sample").remove().end()
								.append(result);
								
						location.hash = "#pageregionid=" + pageregionid + "&itemid=" + showcaseid + "&samplenum=" + samplenum;
					}
				});
	}
};

$(document).ready(function () {
	window.SMS_CREATIVESHOWCASE.init();
});
