
/*LIGHT BOX OBJECT*/
var CDN_LB = {
	div_id:'cdn_lb',//ID for lightbox div
	content_id:'content_lb',
	close_id:'close_lb',
	
/*INIT FUNC*/	
	init:function(){
	
	var _self = this;
	
	//Creating LightBox element and appending it to the dom
	$('<div></div>')
	.attr('id',this.div_id)
	.css({
		'background-color':'#000000',
		'display':'none',
		'height':'100%',
		'left':0,
		'position':'fixed',
		'opacity':0,
		'top':0,
		'width':'100%',
		'z-index':900000
		})
	.prependTo('body'); //LightBox div for fullscreen mode. 
	
	//Creating LightBox  container element and appending it to the light Box wrapper
	$('<div></div>')
	.attr('id',this.content_id)
	.css({
		'background-color':'#D9C7D5',
		'display':'none',
		'position':'absolute',
		'top':0,
		'left':0,
		'width':'200px',
		'height':'200px',
		'z-index':910000  //Greater than LightBox container
		})
	.appendTo('body'); //Appending to lightbox conteiner
	
	$('<a></a>')
	.attr('id',this.close_id)
	.css({
		'position':'absolute',
		'top':0,
		'display':'block',
		'right':0,
		'width':'50px',
		'height':'30px',
		'z-index':915000  //Greater than LightBox container
		})
	.appendTo('#'+this.content_id); //Appending to lightbox conteiner
	
	//Adding Resize Event Listener
	$(window).resize(function() {
	_self.center_elems();//Centering element
	});
	
	//Adding UI Click Events
	$('#'+this.div_id+','+'#'+this.close_id).click(function(e){
	  _self.close();
 	})
	
	}, //END of INIT FUNC
	
/*TOGGLE_LB FUNC*/ //Turn On || Off Light Box with HTML markup inside
	toggle:function(content){
		
		var _self = this;
	
		if (!$('#'+this.div_id).length>0)
		{
			 _self.init();
		}
		
	    var wrap = $('#'+this.div_id);
  		var cont = $('#'+this.content_id);
		
		if( wrap.css('display')!= "block")
		{
			
			_self.add_content(content)	//Adding Content to the div container
			wrap//Doing stuff with the cont conteiner
			.show()
			.animate({opacity:0.5},'normal','swing',function(){
			 cont.show();
			$('html, body').animate({scrollTop:0}, 'slow');
			
			});
			
		}
		else{
			
			_self.close();
		}
     
	return this; 
	 
	}, //END of INIT FUNC
	
/*ADD_CONTENT FUNC*/ //Turn On || Off Light Box with HTML markup inside
	add_content:function(content){
		
		var _self = this;
		
		var c_width = parseInt( $(content).css('width'));
		var c_height = parseInt( $(content).css('height'));
		
		
		
		$("#"+_self.content_id).css({
	
		  'width':c_width,
		  'height':c_height
		  
		});
		
		if( $(content).length > 0  ) //If full_screen is not on 
		{  
			$(content)
			.clone()
			.css('display','block')
			.appendTo("#"+_self.content_id);
			_self.center_elems();//Centering element
			
			return true;
			
		}else{
			return false;	
		}
		
	},
/*Center Elems FUNC*/ //Turn On || Off Light Box with HTML markup inside	
	center_elems:function(){
		var _self = this;
		var w_width  = $(window).width();
		var w_height = $(window).height();
		var cont = $('#'+this.content_id)
		
		//Center gallery nav buttons
		var h_cont_center = Math.round((w_width - parseInt( cont.css('width') ) )/2);
		var v_cont_center = Math.round((w_height - parseInt( cont.css('height') ) )/2);
		
		cont.css({'left':h_cont_center+'px'})
		cont.css({'top' :v_cont_center+'px'})
    },
	
	close:function(){
	 
	 var _self = this;
	 var wrap = $('#'+this.div_id);
	 var cont = $('#'+this.content_id);
	
			wrap
			.hide()
			.css('opacity',0);
			
			cont
			.hide();
			//Delete all elements except for the close box
			cont.children().not('#close_lb').remove();
			
		
	}
	
	
}//END of CDN_LB obj


/*AUTHENTICATED CHECK OBJECT*/

var CDN_Auth = {

    cookie_name:'cdn_opt_in',
	sign_up_id:'#cdn_sign_up',
    white_list: [
				"clubdenoveleras",
				"clubdenoveleras/cdn_novelas",
				"clubdenoveleras/cdn_muro_social",
				"clubdenoveleras/cdn_noticias_y_eventos",
				"clubdenoveleras/cdn_nuestro_blog",
				"clubdenoveleras/cdn_puntos_y_premios",
				"clubdenoveleras/cdn_ayuda",
				"clubdenoveleras/cdn_videos",
				"clubdenoveleras/cdn_fotos",
			],
   //CHECK Function //Checks that user has an CDN user. 
   check:function(){
		if($.cookie(this.cookie_name) == null || $.cookie('snas') == null){
			var p = window.location.pathname;
						
			regex_wall = CDN_Auth.white_list.join('|');
			regex_wall = regex_wall.replace(/\//g, "\\/");
			regex_wall = new RegExp('^\/('+regex_wall+')\/?$', "i");
					
			if ( p.match(regex_wall) == null)
			{
			window.location= "/clubdenoveleras/#?sign_up=true&redirecturl="+document.location.href;
			}
			
			
			/*
			for(x=0;x<this.CDN_auth_path.length;x++){
				var c = this.CDN_auth_path[x];
				if( p.match(new RegExp(c+"\/.+" ))){
					//window.location= "/clubdenoveleras/#?sign_up=true&redirecturl="+document.location.href;
				}
			}
			
			*/
		}
	}
}

CDN_Auth.check();
	
	//Checking for query_string for sign in light box
	if($.address){
		$.address.change(function(e){
		
		if(e.parameters.sign_up === "true"){
		
		//alert($("#cdn_to_register").attr('href'));
		$("#cdn_to_register").attr('href',$("#cdn_to_register").attr('href')+'redirectUrl='+e.parameters.redirecturl);
		$("a#cdn_to_login").each(function(){
			$(this).attr('href',$(this).attr('href')+'redirectUrl='+e.parameters.redirecturl);
		});
		//$("a#cdn_to_login").attr('href',$("#cdn_to_login").attr('href')+'redirectUrl='+e.parameters.redirecturl);
		CDN_LB.toggle('#cdn_sign_up')
		//alert($("#cdn_to_register").attr('href'));


		
		}
		})
	}

$(document).ready(function(){

	
	
});

