/*************** START: Utility Helper **************/
function UtilityHelper() { };
UtilityHelper = {
	format: function(string, arr) {
        try {
            if (!arr) { return string; }
            for (var index = 0; index < arr.length; index++) {
                string = string.replace("{" + index + "}", arr[index]);
            }
            return string;
        } catch (e) {
            throw e;
        }
    },
    apply: function(o, c, defaults) {
        if (defaults) {
            // no "this" reference for friendly out of scope calls
            Ext.apply(o, defaults);
        }
        if (o && c && typeof c == 'object') {
            for (var p in c) {
                o[p] = c[p];
            }
        }
        return o;
    },
    override: function(origclass, overrides) {
        if (overrides) {
            var p = origclass.prototype;
            for (var method in overrides) {
                p[method] = overrides[method];
            }
        }
    },
    extend: function() {
        // inline overrides
        var io = function(o) {
            for (var m in o) {
                this[m] = o[m];
            }
        };
        var oc = Object.prototype.constructor;

        return function(sb, sp, overrides) {
            if (typeof sp == 'object') {
                overrides = sp;
                sp = sb;
                sb = overrides.constructor != oc ? overrides.constructor : function() { sp.apply(this, arguments); };
            }
            var F = function() { }, sbp, spp = sp.prototype;
            F.prototype = spp;
            sbp = sb.prototype = new F();
            sbp.constructor = sb;
            sb.superclass = spp;
            if (spp.constructor == oc) {
                spp.constructor = sp;
            }
            sb.override = function(o) {
                Utility.override(sb, o);
            };
            sbp.override = io;
            Utility.override(sb, overrides);
            sb.extend = function(o) { Utility.extend(sb, o); };
            return sb;
        };
    } ()
}
/*************** END: Utility Helper **************/

/*************** START: URL Parser **************/
// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};
/*************** END: URL Parser **************/

/*************** START: trim function **************/
function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}
function trim(str) {
	return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}
/*************** END: trim function **************/

<!-- SCRIPTS/CSS ROTATOR -->
	$(document).ready( function() {
		/* Start and configuration */	
		$('.jqrotator').jqrotator({
			// Time between images (seconds)
			sleep 		: 10, 
			// Time passed to start again after clicking a button (seconds)
			sleepResume	: 10, 
			// Time of transition (miliseconds)
			duration : 500,	
			// Type of animation (jquery.easing.js required)
			easing : 'easeOutCubic'
		});

<!--// SCRIPTS/CSS ROTATOR -->
<!-- experts remove button -->

$('button').click(function () {
      $(this).parent().fadeOut('slow')
    });
<!-- for login -->

	$('#login_box .closing_but').click(function(e){
		e.preventDefault();
		$('#login_box').slideUp('slow');	  
		});
	$('#forgot_pw_box .closing_but').click(function(e){
		e.preventDefault();
		$('#forgot_pw_box').slideUp('slow');	  
		});
	$('#messageEmail_box .closing_but').click(function(e){
		e.preventDefault();
		$('#messageEmail_box').slideUp('slow');	  
		});
	$('.verLogin').click(function(e){
		e.preventDefault();
		$('#login_box').slideDown('slow');
		});
	$('.verForgot').click(function(e){
		e.preventDefault();
		$('#forgot_pw_box').slideDown('slow');
		
		});

<!-- end login -->

<!-- for articles -->
	// Text expand
	$('a.js-toggle').divtoggle();
	
	// Image gallery
	$('.js-gallery').imageGallery();
    
<!-- end articles -->

<!-- lightbox/fancybox -->
$('a.lightbox').each(function(){
	$(this).fancybox({
			'titlePosition'	: 'inside',
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200
		});
	});
	$('a.setYouTube').fancybox({
			'titlePosition'		: 'inside',
			'autoScale'			: true,
			'width'				: 480,
			'height'			: 390,
			'type' : 'swf',
			'swf' : {
			'wmode' : 'transparent',
			'allowfullscreen' : 'true'
		}
			
		});

		return false;
		

});
<!-- loaded at the end -->
// Cufon.now();

//scripts for articles
// Like button
function fb_like( url ) {
	if (!url) url = window.self.document.location.href;
	document.write('<iframe src="http://www.facebook.com/plugins/like.php?href='+escape(url)+'&amp;layout=button_count&amp;show_faces=true&amp;width=450&amp;action=like&amp;font&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" allowTransparency="true"></iframe>');
}


// Div toggle
$.fn.divtoggle = function() {
	return this.each( function() {
		var target, $target;
		if (!this.href) return;
		if (!(target = this.href.split('#').pop())) return;
		if (!($target = $('#'+target)).size()) return;
		if(trim(document.getElementById(target).innerHTML) == '') {
			$(this).addClass('invisible');
			return;
		}
		$(this).click( function(e) { 
			e.preventDefault(); 
			$target.slideToggle(); 
			if($(this).hasClass('more')) {
				$(this).removeClass('more');
				$(this).addClass('less');
			} else {
				$(this).removeClass('less');
				$(this).addClass('more');
			}
		} );
		$target.hide();
	});
}

// Image gallery
$.fn.imageGallery = function() {
	return this.each( function() {
		var $tar = $(this);
		var $cont = $tar.find('.gallery-items');
		var $thm = $cont.find('a[href]:has(img)');
		var $full = $('<div class="gallery-fullsize"><div class="gallery-fullsize-image"></div><div class="gallery-fullsize-info"></div></div>');
		var $fullimg = $full.find('.gallery-fullsize-image');
		var $fulldesc = $full.find('.gallery-fullsize-info');		
		
		var itemCount = $cont.children().length;
		var swfFile;
		if (itemCount <= 1) {
			$cont.wrap('<div class="gallery-controls invisible"></div>').parent();
			$fullimg.css({'width':'670px'});
			swfFile = 'load-video-article-670.swf';
		}
		else {
			$cont.wrap('<div class="gallery-controls"></div>').parent();
			swfFile = 'load-video-article-540.swf';
		}
		$fullimg.css({"overflow":"hidden"});
		//var $pan ;
		var prev;
		var next;
		var $pan = $cont.wrapInner('<div></div>').find('>div:first');
		
		
		//var prev = $('<a class="gallery-button up">Subir</a>');
		//var next = $('<a class="gallery-button down">Bajar</a>');
		if($('.gallery-items').hasClass('arrow')){
		 prev = $('<a class="gallery-button up arrow">Subir</a>');
		 next = $('<a class="gallery-button down arrow">Bajar</a>');
		}
		
		var h = 350;
		
		$tar.addClass('gallery-enabled');
		$cont.css({'height':h+'px','overflow':'hidden'});
		$cont.before(prev);
		$cont.after(next);
		$fullimg.height(h+prev.height()+next.height());
		$tar.append($full);
		
		var openImage = function(e) {
			e.preventDefault();
			var tar = this;
			$fullimg.fadeTo('fast', 0, function() {
				loadImage(tar, function() {
					$fullimg.fadeTo('fast', 1);
					
				});
			});
			$thm.removeClass('selected').css('opacity', .5);
			$(this).addClass('selected').css('opacity', 1);
			//alert($(this).width())
			
		}
		
		var loadImage = function(link, oncomplete) {
             //alert(typeof HTMLVideoElement);
			 var videoTag = null;
            if($(link).parent().find('.gallery-item-footer iframe') && $(link).parent().find('.gallery-item-footer iframe').length>0){
				// iframe video tag
                videoTag = $(link).parent().find('.gallery-item-footer iframe')[0];
				if(videoTag!=null && videoTag){
					$fullimg.empty();
					var ivideoTag = document.createElement('iframe');
					ivideoTag.width = $fullimg.width();
					ivideoTag.height = $fullimg.height();
					videoTag.style.display='none';
					ivideoTag.style.display='';
					$fullimg.append(ivideoTag);
					ivideoTag.src = videoTag.src; 
				}
				
            }else if($(link).parent().find('.gallery-item-footer object') && $(link).parent().find('.gallery-item-footer object').length>0){
				// object video tag. 
                videoTag = $(link).parent().find('.gallery-item-footer object')[0];
				if(videoTag!=null && videoTag){
					$fullimg.empty();
					
					var ivideoTag = UtilityHelper.apply(videoTag,{});
					var html = UtilityHelper.format("<object width={0} height={1}>{2}</object>", [$fullimg.width(),$fullimg.height(), $(ivideoTag).html()]);
					$fullimg.append(html);
					
					videoTag.style.display='none';
					//alert('object');
				}
            }else if($(link).parent().find('.gallery-item-footer embed') && $(link).parent().find('.gallery-item-footer embed').length>0){
				// embed video tag. This case is also used for object video tag in IE 7 ( with assumption that the object tag contains <embed></embed> ); copying the od youtube embed code will fire this case
                videoTag = $(link).parent().find('.gallery-item-footer embed')[0];
				if(videoTag!=null && videoTag){
					$fullimg.empty();
					
					var ivideoTag = document.createElement(videoTag.outerHTML);
					ivideoTag.style.display='';
					$fullimg.append(ivideoTag);
					
					videoTag.style.display='none';
					//alert('embed');
				}
            } else {
				// video link and image
				if ($.browser.mozilla){
					var video = $(link).parent().find('a.gallery-video-ff');
				} else{
					var video = $(link).parent().find('a.gallery-video-link');
				}
				
				$fullimg.empty();

				if (video.size() && typeof HTMLVideoElement != 'undefined') {
					// video link
					var vid = document.createElement('video');
					vid.src = video.attr('href'); 
					vid.width = $fullimg.width();
					vid.height = $fullimg.height();
					vid.controls = true;
					$fullimg.append(vid);
				} else {
					//find the video again -- > mp4 will be selected first
					video = $(link).parent().find('a.gallery-video-link');
					//if(!video.size()) {
					//	video = $(link).parent().find('a.gallery-video-ff');
					//}
					
					if (video.size()) {
						// there is video link but the browser does not support <video> tag --> try to put an swf
						var swf = UtilityHelper.format("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' width='" + $fullimg.width() + "' height='378' title='{1}'>{0}</object> ", 
							["	<param name='movie' value='/Assets/Modules/Article/Article/Articles/" + swfFile + "?_src=" + video.attr('href') + "' /> " +
							"	<!--[if !IE]>--> " + 
							"	<object type='application/x-shockwave-flash' data='/Assets/Modules/Article/Article/Articles/" + swfFile + "?_src=" + video.attr('href') + "' width='" + $fullimg.width() + "' height='378'> " + 
							"		<param name='wmode' value='opaque' /> " +
							"		<param name='play' value='true' /> " +
							"		<param name='LOOP' value='true' /> " +
							"	</object> " +
							"	<!--[if !IE]>-->",
							link.title]);						
						$fullimg.append(swf);
					}
					else {
						// image
						var img = new Image();
						img.title = link.title;
						img.src=link.href;
						$fullimg.append(img);
						if (img.complete) adjustimage(img);
						else img.onload = adjustimage;
						// Download video link
						if (video.size()) { $fullimg.wrap('<a href="'+video.attr('href')+'"></a>'); alert('1');}
						video.css('color','#999');
					}
					
				}
			}

			$fulldesc.html( $(link).parent().find('.gallery-item-footer').html() );		
			oncomplete();
		}
		
		var i = 0;
		var s = $thm.first().height();
		
		var scrollup = function(e) {
			e.preventDefault();
			if (i<=0) return;
			i--;
			scrollmove();
		}
		
		var scrolldown = function(e) {
			e.preventDefault();
			if (-i*s <= - $thm.size()*s + $cont.height()) return;
			i++;
			scrollmove();
		}
		
		var scrollmove = function() {
			$pan.stop().animate({'margin-top':Math.min(0, Math.max(-$thm.size()*s+$cont.height(), -i*s))+'px'});
		}
		//var w= 540;
		var adjustimage = function( ) {

			var img = $fullimg.find('img').get(0);
			//$(img).css({'position':'absolute'});
			
			//if (img != null && img != 'undefined') {
			
				var w = img.width;
				var h = img.height;
				
				var sw = $fullimg.width()/w;
				var sh = $fullimg.height()/h;
				
				if ($.browser.msie && $.browser.version <= 7){
					var  sw = 550/w;
					var sh = 380/h;
				}
				//if (w==670) {
				//	$('.gallery-controls').css('display','none');
				//	$('ul.gallery-items').css('display','none');
				//	sw=670;
				//}
	 
				img.width = Math.min(sw,sh)*w;
				img.height = Math.min(sw,sh)*h;
				//alert('1  '+$fullimg.width())
				if (img.width >= 670) {
					$(img).css( {
						'margin-left':0+'px',
						 'margin-top':Math.round($fullimg.height()/2-(Math.min(sw,sh)*h/2))+'px'
					})
				}
				else{
					   $(img).css( {
									'margin-left':Math.round($fullimg.width()/2-(Math.min(sw,sh)*w/2))+'px',
									'margin-top':Math.round($fullimg.height()/2-(Math.min(sw,sh)*h/2))+'px'
						})
												   
				}
			//}
		}
		
		prev.click( scrollup );
		next.click( scrolldown );
        if (itemCount <= 4) {
			prev.addClass('invisible');
			next.addClass('invisible');
		}

		$fullimg.css('opacity',0);
		$thm.click( openImage ).first().click();
	});
}
