// Smoooooooth Scrolling Class ;)
var  ScrollLinks = Class.extend({
	init: function(cssClass){
		jQuery(document).ready(function() {
			jQuery(cssClass).each(function(){
				jQuery(this).click(function() {
					jQuery.scrollTo(jQuery(this).attr("href"), 1000, {easing:'easeOutExpo'} );
					return false;
				});
			});		
		});
	},
	
	To: function(Where, FunctionAfter) {
		jQuery.scrollTo(Where, 1000, {easing:'easeOutExpo', onAfter: FunctionAfter } );
	}
});

// Smoooooooth Scrolling Class ;)
var  ExternalLinks = Class.extend({
	init: function(cssClass){
		jQuery(document).ready(function() {
			jQuery(cssClass).each(function(){
				jQuery(this).attr("target", "_blank");
			});		
		});
	}
});

// Overlay Box Class
var OverlayBox = Class.extend({
	childrens: {},
	
	init: function() {
		var self = this;
		
		jQuery(document).ready(function() {
			jQuery('<div id="OverlayBox"></div>').prependTo("body:first");
			
			jQuery("#OverlayBox").css({ display: "none", position: "absolute", top: 0, left: 0, zIndex: 99,	background: "#000", opacity: 0 });
		
			jQuery(window).resize(function() {
				self.SetBackgroundSize();
			});
		});
	},
	
	SetBackgroundSize: function() {
		jQuery("#OverlayBox").css({ width: jQuery(window).width(), height: jQuery(document).height() });
	},
	
	ShowBackground: function(onLoadBg) {
		this.SetBackgroundSize();
		jQuery("#OverlayBox").css({ display: "block"});
		jQuery("#OverlayBox").animate({ opacity: .83 }, 600, "easeOutExpo", function() {
			onLoadBg();
		});
	},
	
	HideBackground: function() {
        jQuery("#OverlayBox").animate({ opacity: 0 }, 600, "easeOutExpo", function() {
			jQuery(this).css({ display: "none" });
		});
	},
	
	AddChildren: function(childId, childObj) {
		this.childrens[childId] = childObj;	
	},
	
	ShowChildren: function(childId, FunctionOnLoad, FunctionOnUnLoad) {
		var self = this;
		
		jQuery('<div id="OverlayBoxChildren-' + childId + '" class="OverlayChild"></div>').prependTo("body:first");
		jQuery("#OverlayBoxChildren-" + childId).css({ position: "absolute", zIndex: 100 });
		
		jQuery(this.childrens[childId]).prependTo("#OverlayBoxChildren-" + childId);
		
		this.ShowBackground(function() {
			FunctionOnLoad();
		});
		
		jQuery("#OverlayBox").unbind();
		jQuery("#OverlayBox").click(function() {
			self.HideChildren(childId, function() {
				FunctionOnUnLoad();
			});
		});
	},
	
	HideChildren: function(childId, FunctionOnLoad) {
		var self = this;
		
		jQuery("#OverlayBoxChildren-" + childId).animate({ opacity: 0 }, 200, "easeOutExpo", function() {
			jQuery(this).remove();
			FunctionOnLoad();
			self.HideBackground();
		});
	}
	
	
});

// Carousel Helper Class
var CarouselHelper = Class.extend({
	settings : {
		circular: true,
		speed: 500,
		easing: "easeOutExpo"	
	},
	
	init: function(carousell, settings) {
		var self      = this;
		jQuery.extend(self.settings, settings);
		self.carousel = carousell;
	},
	
	setButtons: function(prev, next) {
		var self      = this;
		jQuery.extend(self.settings, {
			btnNext: prev,
			btnPrev: next
		});
	},
	
	create: function() {
		var self = this;
		jQuery(document).ready(function() {
			$(self.carousel).jCarouselLite(self.settings);
		});
	}	
});

// Controlls Remote Class
var ControllsRemote = Class.extend({
	controlls: {},
	
	init: function() {
		var self = this;
	},
	
	AddControll: function(controll, settingsOn, settingsOff) {
		this.controlls[controll] = { id: controll, on: settingsOn, off: settingsOff, isOn: false };
	},
	
	ShowHide: function(controll) {
		var self = this;
		var wait = false;
		
		if(self.controlls[controll].isOn == false) {
			
			jQuery.each(self.controlls, function(i, _controll) {
				if(_controll.id != self.controlls[controll].id && _controll.isOn == true) {
					jQuery(_controll.id).animate(_controll.off, 600, "easeOutExpo", function() {
						jQuery(_controll.id).css({ display: 'none' })
						self.controlls[_controll.id].isOn = false;
					});
					wait = true;
				}
			});
			
			jQuery(controll).onetime((wait == true ? 600 : 1), function() {
				jQuery(controll).css(self.controlls[controll].off);
				jQuery(controll).animate(self.controlls[controll].on, 600, "easeOutExpo");
				self.controlls[controll].isOn = true;
			});
			
		}else {
			jQuery(controll).animate(self.controlls[controll].off, 600, "easeOutExpo", function() {
				jQuery(controll).css({ display: 'none' })
				self.controlls[controll].isOn = false;
			});
		}	
	}
	
});

// Quick Login Class
var  QuickLogin = Class.extend({
	loginURL: null,
	loginOn: false,
	referer: "",
	
	init: function(LoginBoxContainer) {
		var self               = this;
		self.LoginBoxContainer = LoginBoxContainer;
		
		jQuery(document).ready(function() {
			self.loginURL = jQuery(self.LoginBoxContainer).find("form").attr("action");
			jQuery(self.LoginBoxContainer).find("input[type=submit]").click(function() {		
				self.Verify();
				return false;
			});
		});
	},
	
	Verify: function() {
		var self       = this;
		var emailField = jQuery(self.LoginBoxContainer).find("input[name=email]").val();
		var passField  = jQuery(self.LoginBoxContainer).find("input[name=password]").val();
		
		jQuery.post(self.loginURL.replace("/signin", "/ajax/signin"), { email: emailField, password: passField, referer: self.referer }, function(json) {
			if(json.LoginValidation) {
				jQuery(self.LoginBoxContainer).find("form").attr("action", self.loginURL);
				jQuery(self.LoginBoxContainer).find("form").submit();
			} else {
				jQuery(self.LoginBoxContainer).find("h2:first").animate({ opacity: 0 }, 400, "easeOutExpo", function() {
					jQuery(self.LoginBoxContainer).find("h2:first").removeClass();
					jQuery(self.LoginBoxContainer).find("h2:first").addClass("loginHeaderError ir");	
				});
				jQuery(self.LoginBoxContainer).find("h2:first").animate({ opacity: 1 }, 400, "easeOutExpo");
			}
		}, "json");
	},
	
	Referer: function(referer) {
		var self     = this;
		self.referer = referer;
		jQuery(document).ready(function() {
			jQuery('<input type="hidden" name="referer" value="' + referer + '">').appendTo(jQuery(jQuery(self.LoginBoxContainer).find("form")));
		});
	}
});

// Customization Class
var  Customization = Class.extend({
	init: function(CustomizationBarContainer) {
		var self                       = this;
		self.CustomizationBarContainer = CustomizationBarContainer;
	}
});

// Add Coment Class
var  AddComment = Class.extend({
	commentURL: null,
	loginURL: null,
	
	init: function(CommentContainer){
		var self              = this;
		self.CommentContainer = CommentContainer;
		
		jQuery(document).ready(function() {
			self.commentURL = jQuery(self.CommentContainer).find("form").attr("action");
			jQuery(self.CommentContainer).find("input[type=submit]").click(function() {		
				self.Add();
				return false;
			});
		});
	},
	
	PreloaderShow: function() {
		jQuery("#addYourCommentPreloader").css({ opacity: 0, display: "block" });
		jQuery("#commentText").css({ display: "none" });
		jQuery("#addYourCommentPreloader").animate({ opacity: 1 }, 400, "easeOutExpo");
	},
	
	PreloaderHide: function() {
		jQuery("#commentText").css({ opacity: 0, display: "block" });
		jQuery("#addYourCommentPreloader").css({ display: "none" });
		jQuery("#commentText").animate({ opacity: 1 }, 400, "easeOutExpo");
	},
	
	Add: function() {
		var self         = this;
		var commentField = jQuery(self.CommentContainer).find("textarea[name=content]").val();
		
		self.ErrorHide();
		
		if(commentField == "") { 
			self.ErrorShow("empty");
			return; 
		}
		
		self.PreloaderShow();
				
		jQuery.post(self.commentURL.replace("/comment", "/ajax/comment"), { content: commentField }, function(json) {
			if(!json.Comment) {
				self.ErrorShow("login");
				self.PreloaderHide();
			}else {
				jQuery("#commentText").val("");
				self.Create(json.Comment);
				self.PreloaderHide();
			}
		}, "json");
	},
	
	Create: function(Comment) {
		var list     = document.createElement("li");
		var list_h3  = document.createElement("h3");
		var list_h4  = document.createElement("h4");
		var list_img = document.createElement("img");
		var list_bq  = document.createElement("blockquote");
		
		list.appendChild(list_h3);
		list.appendChild(list_h4);
		list.appendChild(list_img);
		list.appendChild(list_bq);
		
		jQuery(list).find("h3:first").html(Comment.user_nickname);
		jQuery(list).find("h4:first").html(Comment.user_date);
		jQuery(list).find("img:first").attr("src", Comment.user_avatar);
		jQuery(list).find("img:first").attr("alt", Comment.user_nickname);
		jQuery(list).find("blockquote:first").attr("cite", Comment.user_nickname);
		jQuery(list).find("blockquote:first").html(Comment.user_content);
		
		//jQuery(list).attr("id", "CommentId-" . Comment.id);
		
		jQuery(list).prependTo("#listComments");
		Scroll.To("#Comments");
	},
	
	ErrorShow: function(error) {
		var self = this;
		
		if(error == "login") {
			jQuery("#addYourCommentError").click(function() {
				self.loginURL();
				self.ErrorHide();
			});
		}
		
		jQuery("#addYourCommentError").removeClass();
		jQuery("#addYourCommentError").addClass("error_" + error);
		jQuery("#addYourCommentError").css({ opacity: 0, display: "block", height: 0 });
		jQuery("#addYourCommentError").animate({ opacity: 1, height: 65}, 600, "easeOutExpo");	
	},
	
	ErrorHide: function(error) {
		jQuery("#addYourCommentError").unbind();
		jQuery("#addYourCommentError").removeClass();
		jQuery("#addYourCommentError").css({ display: "none" });	
	},
	
	setLoginLink: function(link) {
		this.loginURL = link;
	}
});

var OverlayGallery = Class.extend({
	JsonImagesURL: null,
	Images:        {},
	Ids:           {},
	CurrentImg:    {},
	OverlayObject: null,
	GalleryId:     null,
	Total:         0,
	
	init: function(JsonImagesURL, GalleryId, OverlayObject) {
		var self           = this;
		self.JsonImagesURL = JsonImagesURL;
		self.OverlayObject = OverlayObject;
		self.GalleryId     = GalleryId;
		
		jQuery(document).ready(function() {	
			jQuery.getJSON(self.JsonImagesURL, function(Images) {
				jQuery.each(Images.images, function(i, Img) {
		        	self.Images[Img.id] = { image : Img.image, title : Img.title };
					self.Ids[i]         = Img.id;
					self.Total          = self.Total + 1;
				});
			});
		
			self.OverlayObject.AddChildren("Gallery-" + self.GalleryId, "<div class=\"GalleryDesc\"></div><a href=\"#\" class=\"GalleryPrev\">Previous</a><img /><a href=\"#\" class=\"GalleryNext\" >Next</a>");
		});
	},
	
	Show: function(ImgId) {

		var self = this;
		
		self.OverlayObject.ShowChildren("Gallery-" + self.GalleryId, function() {
					
			jQuery("#OverlayBoxChildren-Gallery-" + self.GalleryId).css({
				position:   "absolute", 
				border:     "10px solid #fff",
				width:      1,
				height:     1,
				left:       '50%'
			});
			
			jQuery("#OverlayBoxChildren-Gallery-" + self.GalleryId).find("img").fadeOut();
			
			jQuery("#OverlayBoxChildren-Gallery-" + self.GalleryId).find(".GalleryPrev").click(function(){
				self.Prev();	
				return false;
			});
			
			jQuery("#OverlayBoxChildren-Gallery-" + self.GalleryId).find(".GalleryNext").click(function(){
				self.Next();
				return false;
			});
			
			jQuery("#OverlayBoxChildren-Gallery-" + self.GalleryId).find(".GalleryDesc").click(function(){
				self.Hide();
				return false;
			});

			self.LoadImage(ImgId);
		}, function() {

		});
	},
	
	LoadImage: function(ImgId) {
		var self         = this;
		var ImageGallery = new Image();
		self.CurrentImg  = ImgId;
			
		ImageGallery.onload = function() {
			var topPX = jQuery(window).scrollTop() + ((jQuery(window).height() - ImageGallery.height ) / 2);
			var topPX = (ImageGallery.height > jQuery(window).height()) ? topPX : jQuery(window).scrollTop() + 100;
			
			jQuery("#OverlayBoxChildren-Gallery-" + self.GalleryId).find(".GalleryNext, .GalleryPrev").css({
				position: 'absolute',
				top:      -10,
				display:  'block',
				width:    100,
				height:   (ImageGallery.height + 20)
			});
			
			jQuery("#OverlayBoxChildren-Gallery-" + self.GalleryId).find(".GalleryPrev").css({
				left: -110
			});
			
			jQuery("#OverlayBoxChildren-Gallery-" + self.GalleryId).find(".GalleryNext").css({
				right: -110	
			});
			
			jQuery("#OverlayBoxChildren-Gallery-" + self.GalleryId).find(".GalleryDesc").css({
				position: 'absolute',
				width:    (ImageGallery.width + 20),
				top:      -40,
				left:     -10,
				height:    30
			});
			
			jQuery("#OverlayBoxChildren-Gallery-" + self.GalleryId).find(".GalleryDesc").html(self.Images[ImgId].title);
			
			jQuery("#OverlayBoxChildren-Gallery-" + self.GalleryId).css({
				top:  		topPX
			});
							
			jQuery("#OverlayBoxChildren-Gallery-" + self.GalleryId).animate({
				width:      ImageGallery.width, 
				height:     ImageGallery.height,
				marginLeft: -(ImageGallery.width / 2)
			}, 1000, "easeOutExpo", function() {
				jQuery("#OverlayBoxChildren-Gallery-" + self.GalleryId).find("img").attr("src", self.Images[ImgId].image).unbind();
				jQuery("#OverlayBoxChildren-Gallery-" + self.GalleryId).find(".GalleryNext, .GalleryPrev, .GalleryDesc, img").fadeIn();
			});
		}
	
		ImageGallery.src = self.Images[ImgId].image;	
	},
	
	Next: function() {
		var self = this;
		var stop = false; 
		jQuery("#OverlayBoxChildren-Gallery-" + self.GalleryId).find("img").fadeOut();
		jQuery.each(self.Ids, function(i, Id) {
			if(Id == self.CurrentImg && stop == false) {
				if(typeof self.Ids[(parseInt(i) + 1)] == 'undefined') {
					self.LoadImage(self.Ids[0]);
					stop = true;
				}else{
					self.LoadImage(self.Ids[(parseInt(i) + 1)]);
					stop = true;
				}
			}
		});
	},
	
	Prev: function() {
		var self = this;
		var stop = false; 
		jQuery("#OverlayBoxChildren-Gallery-" + self.GalleryId).find("img").fadeOut();
		jQuery.each(self.Ids, function(i, Id) {
			if(Id == self.CurrentImg && stop == false) {
				if(typeof self.Ids[(parseInt(i) - 1)] == 'undefined') {
					self.LoadImage(self.Ids[(self.Total - 1)]);
					stop = true;
				}else{
					self.LoadImage(self.Ids[(parseInt(i) - 1)]);
					stop = true;
				}
			}
		});
	},
	
	Hide:  function() {
		this.OverlayObject.HideChildren("Gallery-" + this.GalleryId, function() {

		});	
	},    
	
	Cover: function(bool) {
		var parent = this.overlayGalleryParent;
		document.getElementById(parent.replace("#", "")).coverHandle(bool);
	}
	
});

var PlayerHelper = Class.extend({
	init: function() {
		return;
	},
	
	PlaySong: function(TrackId) {
		document.getElementById('flash-menu-object').SetVariable('jscriptIncoming', TrackId);
		document.getElementById('flash-menu-object').TCallLabel('/','jscript');
	}
});

var TimestampConvert = Class.extend({	
	
	vars: { request: null },
	
	/* ****************************************************************************************************************** */
	
	init: function(request) {
		this.vars.request = request;
		var self          = this;
		
		jQuery(window).ready(function() {
			jQuery('.TimestampConvert').each(function() {
			
				jQuery("<input type=\"hidden\" />")
				.attr("name", jQuery(this).attr("name"))
				.attr("id", jQuery(this).attr("id"))
				.val(jQuery(this).val())
				.appendTo(jQuery(this).parent());
				
				jQuery(this).remove();
				
				self.drawTimestampInfo(jQuery("#" + jQuery(this).attr("id")));	
			});					
		});
	},
	
	/* *********************************************************************************************************************
	
	DRAW
	
	********************************************************************************************************************* */
	
	drawTimestampInfo: function(dateObj) {
		// Define what type of SELECT we are using ...
		if (jQuery(dateObj).hasClass('gcms_formTimestampYmdhi')) {
			var type = 'ymdhi';
		} else {
			var type = 'ymd';	
		}
		
		// Set Self
		var self    = this;
		var dateObj = jQuery(dateObj);
		
		// Construct an AJAX request to get the file information ...
   		jQuery.post(this.vars.request, { dates: type, name: dateObj.attr('name'), value: dateObj.val() }, function(HTML) {
			jQuery(HTML).appendTo(dateObj.parent());
			jQuery('select[rel=' + dateObj.attr('name') + ']').each(function() {					
				jQuery(this).change(function() {
					self.onChangeSelect(this);				
				});
			});				
		});
	},
	
	/* *********************************************************************************************************************
	
	CHANGE
	
	********************************************************************************************************************* */
	
	onChangeSelect: function(ddObj) {
		// Set ddObj
		var ddObj = jQuery(ddObj);
		
		// Build up our value string based on the new value ...
		var v = jQuery('select[name=' + ddObj.attr('rel') + '_y]:first').val();
		v += '-';
		v += jQuery('select[name=' + ddObj.attr('rel') + '_m]:first').val();
		v += '-';
		v += jQuery('select[name=' + ddObj.attr('rel') + '_d]:first').val();
		v += ' ';
		v += jQuery('select[name=' + ddObj.attr('rel') + '_h]:first, input[name=' + ddObj.attr('rel') + '_h]:first').val();
		v += ':';
		v += jQuery('select[name=' + ddObj.attr('rel') + '_i]:first, input[name=' + ddObj.attr('rel') + '_i]:first').val();
		v += ':00';		
		
		// update the (now) hidden field ...
		jQuery('input[name=' + ddObj.attr('rel') + ']:first').val(v);
	}
	
	/* ****************************************************************************************************************** */
});