/**
* reseda JavaScript stuff.
* @author CNPP
*/
var reseda = {
	
	/**
	* Initialize.
	*/
	initOnReady: function() {	
		
		// context
		var ctxPage = jQuery("#page");
		var ctxHeader = jQuery("#header");
		var ctxContent = jQuery("#content");
		var ctxMarginal = jQuery("#content");
		var ctxMain = jQuery("#main");
		var ctxNewsletter = jQuery("#newsletter");
		
		// init all
		jQuery().browserSupport();
		reseda.initToggler(ctxPage);
		reseda.initSlides(ctxPage);
		reseda.initClipboard(ctxMarginal);
		reseda.initResedesigner(ctxMain);
		reseda.initForms(ctxContent);
		reseda.initTabs(ctxContent);
		reseda.initFilter(ctxContent);
		reseda.initHoverable(ctxContent);
		reseda.initModal(ctxMain);
		reseda.initNewsletter(ctxNewsletter);
		//reseda.initNotifications(ctxPage);
		reseda.initPrintLinks(ctxHeader);
	},
	
	/**
	* Initialize the togglers.
	*/
	initToggler: function(ctx) {	
		
		// togglers
		jQuery(".toggler",ctx).each(function(ind,el) {
			// vars
			var id = jQuery(el).attr("id");
			var msgExpanded = null;
			var msgCollapsed = null;
			var expanded = false;
			var title = jQuery(this).attr("title");
			if (jQuery(el).hasClass("expanded")) {
				expanded = true;	
			}
			if (title.indexOf("$") >= 0) {
				var msgs = title.split("$");
				msgCollapsed = msgs[0];
				msgExpanded = msgs[1];
			}
			
			// toggle
			jQuery(el).toggler("."+id,{expanded:expanded,msgCollapsed:msgCollapsed,msgExpanded:msgExpanded,animate:true});
											 
		});
	},
	
	/**
	* Initialize slides.
	*/
	initSlides: function(ctx) {			
		// slides
		jQuery(".slides",ctx).each(function(i,el){
			jQuery(el).slides();
		});
	},
	
	/**
	* Initialize the clipboard.
	*/
	initClipboard: function(ctx) {			
		// clipboard
		jQuery("#box_clipboard",ctx).clipboard();
	},
	
	/**
	* Initialize tabs.
	*/
	initTabs: function(ctx) {
		// tab
		jQuery("#tabs",ctx).tabs({cookie:{expires:30}});
	},
	
	/**
	* Initialize filters.
	*/
	initFilter: function(ctx) {	
		
		// filters
		jQuery(".filter",ctx).each(function(i,el){
			jQuery(el).filterTabs();
		});
	},
	
	/**
    * Initializes the resedesigner.
    */
	initResedesigner: function(ctx) {	     
     jQuery(".resedesigner",ctx).each(function(e) {
		var e = jQuery(this);
         var url = jQuery(e).attr("href");
		jQuery(e).popupWindow(url, {width:"1024", height:"768",left:"50",top:"50"});
     });
    },
	
	/**
	* Initialize the forms.
	*/
	initForms: function(ctx) {
		
		// slider
		jQuery(".slider",ctx).each(function(i,el){			
			jQuery(el).cslider();
		});
		
		// styled dropdowns
		jQuery(".dropdown",ctx).each(function(i,el){
			jQuery(el).dropdown({"zIndex":10-i});
		});
		
		// anchor
		var elAnchor = jQuery("form #anchor")
		if (elAnchor.size() > 0) {
			var v = elAnchor.val();
			if (v != null && v != "") {
				location.href = location.href+"#"+ v;
			}
		}
		
	},
	
	
	/**
	 * Initialize the modal.
	 */
	initModal: function(ctx){
		jQuery(".modal",ctx).nyroModal({
			bgColor:'#000000',
			minWidth:'800',
			minHeight:'600',
			closeSelector:'#closeBut, .modal_close',
			hideContent:function hideModal(elts, settings, callback) {
			  elts.wrapper.hide().animate({opacity: 0}, {complete: callback, duration: 80}); 
			},
			showBackground:function showBackground(elts, settings, callback) {
				elts.bg.css({opacity:0}).fadeTo(300, 0.75, callback);
			},
			endShowContent:function endShowContent() {
				reseda.initSlides();
			}
		});
	},
	
	/**
	* Initialize the hoverables.
	*/
	initHoverable: function(ctx) {	
		
		// hoverables
		jQuery(".hoverable",ctx).each(function(i,el){
			var hoverables = jQuery(el).children();
			
			// events
			jQuery(hoverables).bind("mouseenter",function(){
				jQuery(this).addClass("hover");
				jQuery("img",this).animate({opacity: 0.9}, 180);
			});
			jQuery(hoverables).bind("mouseleave",function(){
				jQuery(this).removeClass("hover");
				jQuery("img",this).animate({opacity: 1.0}, 300);
			});
			jQuery(hoverables).bind("click",function(){
				var h = jQuery("a",this).attr("href");
			    window.location.href = h;
				return false;
			});
		});
	},
	
	/**
	* Initialize the newsletter.
	*/
	initNewsletter: function(ctx) {	
		
		// slides
		jQuery("#newsletter_email",ctx).fieldEnhancer("NewsletterSubscriptionForm","#form_newsletter",{"fieldValue":"Email Adresse eintragen"});
	},
	
	/**
	* Initialize the notifications.
	*/
	initNotifications: function(ctx) {	
		
		// category
		if (jQuery("#page").hasClass("template_category")) {
			
			// Ausstellungsverkauf
			jQuery("#page").notification({
				triggerURL:true,
				keywordURL:"catId=49",
				selectorHide:"#filter_produkte, #subnavigation ul.leaves",
				selectorAppend:"#content",
				notificationMsg:"Besuchen Sie unseren aktuellen Ausstellungsverkauf in Spreitenbach, Winterthur und Z&uuml;rich bis 6. August 2011."
			});
		}
	},
	
	/**
	* Initialize the print links.
	*/
	initPrintLinks: function(ctx) {	
		jQuery("#meta_print a, .print",ctx).bind("click",function(){
			window.print();
		})
	}
}
jQuery(document).ready(function(){
	reseda.initOnReady();
});
