﻿var ME_COUNT = 0;
var ME_TYPE_IMAGE = 1;
var ME_TYPE_TEXT = 2;
var ME_TYPE_FLASH = 3;
var ME_TYPE_HTML=4;
var ME_TYPE_IFRAME=4;
function MetaNET_Media(args) {
    var __rendered = 0;
    ME_COUNT++;
    this.__id = ME_COUNT;
    this.Id = 'MED_NUMBER_' + ME_COUNT;
    this.name = (typeof(args['name']) == 'undefined' || args['name'] == null) ?  this.Id : args['name'];
    this.imageUrl = (typeof(args['imageUrl']) == 'undefined' || args['imageUrl'] == null) ? '': args['imageUrl'];
    this.linkUrl = (typeof(args['linkUrl']) == 'undefined' || args['linkUrl'] == null) ? '#': args['linkUrl'];
    this.width = (typeof(args['width']) == 'undefined' || args['width'] == null) ? 0: args['width'];
    this.height = (typeof(args['height']) == 'undefined' || args['height'] == null) ? 0: args['height'];
    this.target = '_self';
    this.wmode =  'transparent';
    this.IS_NO_LINK = (this.linkUrl == '' || this.linkUrl == '#' || this.linkUrl == 'javascript:void(0);' || this.linkUrl == 'about:blank' || typeof(this.linkUrl) == 'undefined');
    if ((typeof(args['meType']) == 'undefined' || args['meType'] == null) && !(typeof(args['imageUrl']) == 'undefined' || args['imageUrl'] == null)) {
        var t = new String(this.imageUrl);
		t = t.toLowerCase();
		if(t.lastIndexOf('.swf')>=0){
	      this.meType=ME_TYPE_FLASH;
	    }
	    else if((t.lastIndexOf('.gif')>=0)
				||(t.lastIndexOf('.jpg')>=0)
				||(t.lastIndexOf('.png')>=0))
		{
	      this.meType=ME_TYPE_IMAGE;
	    }
		else if((t.lastIndexOf('.htm')>=0)
				||(t.lastIndexOf('.asp')>=0)
				||(t.lastIndexOf('.php')>=0)
				||(t.lastIndexOf('.html')>=0)
				||(t.lastIndexOf('.aspx')>=0)
				||(t.lastIndexOf('.ashx')>=0))
		{
		  this.meType=ME_TYPE_HTML;
		}
	    else{
	      this.meType=ME_TYPE_TEXT;
	    }
    } else {
        this.meType = args["meType"];
    }
};
/*Hien thi quang cao*/
MetaNET_Media.prototype = {
    renderHTML: function() {
        var s = "";
        if (this.meType == ME_TYPE_IMAGE) {
            if (this.IS_NO_LINK) {
                s = "<div id='" + this.Id + "'><img src='" + this.imageUrl + "' width='" + this.width + "' height='" + this.height + "' border=0><img style='position:absolute; top:0px; left:0px;z-index:1' " + "src='" + __getViewLink(this.name) + "' border='0' width='1' height='1'/></div>";
            } else {
                s = "<div id='" + this.Id + "' width='" + this.width + "' height='" + this.height + "'><a href = '" + this.linkUrl + "' target='" + this.target + "'>" + "<img src='" + this.imageUrl + "' width='" + this.width + "' height='" + this.height + "' border='0'/></a><img style='position:absolute; top:0px; left:0px;z-index:1;width:1px!important;height:1px!important' " + "src='" + __getViewLink(this.name) + "' border='0' width='1' height='1'/></div>";
            }
        } else if (this.meType == ME_TYPE_FLASH) {
            if (this.IS_NO_LINK) {
                s = "<div style='width:" + this.width + "px; height:" + this.height + "px;' id='" + this.Id + "' >" + "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' " + " codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0' " + " width=" + this.width + " height=" + this.height + " id='" + this.Id + "_FLASH' align='middle'>" + " <param name='allowScriptAccess' value='sameDomain' />" + " <param name='movie' value='" + this.imageUrl + "'/>" + " <param name='wmode' value='" + this.wmode + "' />" + " <param name='linkUrl' value='" + this.linkUrl + "' />" + " <param name='quality' value='high' />" + "<embed src='" + this.imageUrl + "'" + " quality='high' width=" + this.width + " height=" + this.height + " id='" + this.Id + "_FLASH'" + " name='" + this.Id + "_FLASH'" + " align='middle' allowScriptAccess='sameDomain'" + " type='application/x-shockwave-flash'" + " wmode='" + this.wmode + "'" + " linkUrl='" + this.linkUrl + "'" + " pluginspage='http://www.macromedia.com/go/getflashplayer' />" + " </object><img style='position:absolute; top:0px; left:0px;z-index:1' " + "src='" + __getViewLink(this.name) + "' border='0' width='1' height='1'/></div>";
            } else {
                s = "<div style='width:" + this.width + "px; height:" + this.height + "px;'>" + "<div style='position:relative;width:" + this.width + "px; height:" + this.height + "px;' id='" + this.Id + "'>" + "<a href='" + this.linkUrl + "' target='" + this.target + "'>" + "<div style='position:absolute; top:0px; left:0px;cursor:pointer;" + "width:" + this.width + "px; height:" + this.height + "px;z-index:3;display:block;background-color:Transparent'></div>" + "<img style='position:absolute; top:0px; left:0px;cursor:pointer;z-index:4' " + "src='/images/spacer.gif' border=0 width=" + this.width + " height=" + this.height + "/>" + "</a>" + "<div style='position:absolute; top:0px; left:0px;cursor:pointer;" + "width:" + this.width + "px; height:" + this.height + "px;z-index:2;background-color:transparent;'>" + "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' " + " codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0' " + " width=" + this.width + " height=" + this.height + " id='" + this.Id + "_FLASH' align='middle'>" + " <param name='allowScriptAccess' value='sameDomain' />" + " <param name='movie' value='" + this.imageUrl + "'/>" + " <param name='wmode' value='" + this.wmode + "' />" + " <param name='linkUrl' value='" + this.linkUrl + "' />" + " <param name='quality' value='high' />" + "<embed src='" + this.imageUrl + "'" + " quality='high' width=" + this.width + " height=" + this.height + " id='" + this.Id + "_FLASH'" + " name='" + this.Id + "_FLASH'" + " align='middle' allowScriptAccess='sameDomain'" + " type='application/x-shockwave-flash'" + " wmode='" + this.wmode + "'" + " linkUrl='" + this.linkUrl + "'" + " pluginspage='http://www.macromedia.com/go/getflashplayer' />" + " </object></div></div></div>";
            }
        } else if (this.meType == ME_TYPE_HTML) {
			s="<div style='width:"+this.width+"px; height:"+this.height+"px;'>"+
			"<iframe style='width:"+this.width+"px; height:"+this.height+"px;' "+
			"height='"+this.height+"' frameborder='0' width='"+this.width+"' "+
			"scrolling='no' src='"+this.imageUrl+"' marginwidth='0' marginheight='0' "+
			"vspace='0' hspace='0' allowtransparency='true'></iframe><img style='position:absolute; top:0px; left:0px;z-index:1' " + "src='" + __getViewLink(this.name) + "' border='0' width='1' height='1'/></div>";
		} else {
            if (this.IS_NO_LINK) {
                s = this.imageUrl+
                "<img style='position:absolute; top:0px; left:0px;z-index:1' " + "src='/images/spacer.gif' border='0' width='1' height='1'/>";
            } else {
                s = "<a href = '" + this.linkUrl + "' id='" + this.Id + "'  target='" + this.target + 
                "' style='display:block;height:" + this.height + "px;width:" + this.width + 
                "px;border:solid 1px #ccc; foat:left;'>" + this.imageUrl + 
                "</a><img style='position:absolute; top:0px; left:0px;z-index:1' " + 
                "src='/images/spacer.gif' border='0' width='1' height='1'/>";
            }
        }
        __rendered = 1;
        return s;
    },
    show: function() {
        if (__rendered == 1) {
            var adDOMObj = __getEL(this.Id);
			adDOMObj.style.display = 'inline';
        } else {
            alert("[MetaNET_Media]:" + 'Khong the hien thi MetaNET_Media co ID=' + this.Id + ' do chua duoc Render');
        }
    },
    hide: function() {
        try {
            var adDOMObj = __getEL(this.Id);
			adDOMObj.style.display = 'none';
        } catch(e) {}
    },
    renderIn: function(domId) {
        __getEL(domId).innerHTML = this.renderHTML();
    },
    renderOut: function() {
        document.write(this.renderHTML());
    }
};


YAHOO.namespace("MetaNET");
YAHOO.MetaNET.slideshow = function (container, o) {
	this.container = YAHOO.util.Dom.get(container);
	this.effect = o.effect;
	var frames = o.frames;
	this.frames = [];
	//add cached frames
	var cached_frames = YAHOO.util.Dom.getElementsByClassName("yui-sldshw-frame", null, this.container);
	
	for (var i=0; i<cached_frames.length; i++)
	{
		this.frames[i] = { id: i, type: 'cached', value: cached_frames[i]};
	}

	if (frames != null && frames!=undefined)
	{
		for (var i=0; i<o.frames.length; i++)
		{
			this.frames[i+cached_frames.length] = o.frames[i];
		}
	}

	//set slide selector
	if (! o.slide_selector)
	{
		this.slide_selector = function(number_of_slides, current_index)
			{
				//return Math.floor( Math.random()*number_of_slides);
				return (current_index+1)%number_of_slides;
			}

	}
	else
	{
		this.slide_selector = o.slide_selector;
	}

	if (o.interval)
		this.interval = o.interval
	else
		this.interval = 5000




	this.init();

}


YAHOO.MetaNET.slideshow.prototype = {
	init: function()
		{
			if (! this.effect)
			{
				this.effect= YAHOO.MetaNET.slideshow.effects.slideUp;
			}
			
			this.active_frame = this.get_active_frame();
			this.choose_next_frame();
		},
	get_active_frame: function()
		{
			var current_frame =  YAHOO.util.Dom.getElementsByClassName("yui-sldshw-active", null,  this.container)[0];
			return current_frame;
		},
	get_frame_index: function(frame)
		{
			for(var i=0; i<this.frames.length;i++)
			{
				if (this.frames[i].value==frame)
					return i;
			}
			return -1;
		},
	choose_next_frame : function( reverse )
		{
			var reverse = (reverse ==null) ? false : true ;
			var current_index = this.get_frame_index(this.get_active_frame());
			if (current_index<0)
				current_index=0;
			var all_frames = this.frames;
			if (reverse)
			{
			    var next_index = current_index -1;
				if (next_index < 0)
					next_index = all_frames.length + next_index;
				YAHOO.util.Dom.replaceClass(YAHOO.util.Dom.getElementsByClassName("yui-sldshw-next")[0], "yui-sldshw-next", "yui-sldshw-cached");
			}
			else
			{
				var next_index = this.slide_selector(all_frames.length, current_index);
			}
			var next = all_frames[next_index];
			var next_frame;
			//possible infinite loop....
			while (next.value==this.active_frame || next.type=="broken")
			{   
				next = all_frames[this.slide_selector(all_frames.length, next_index)];
			}
			if (next.type=='cached')
			{
				next_frame = next.value;
				YAHOO.util.Dom.replaceClass(next_frame, "yui-sldshw-cached", "yui-sldshw-next");
				this.next_frame = next_frame;
				this.effect.setup(this.next_frame);
			}
			else if ( next.type=='image_url')
			{
				next_frame = document.createElement('img');
				next_frame.setAttribute('src',next.value);
				//next_frame.setAttribute('id','frame_'+next.id);
				next.type='cached';
				next.value=next_frame;
				YAHOO.util.Dom.addClass(next_frame, "yui-sldshw-frame");
				YAHOO.util.Dom.addClass(next_frame, "yui-sldshw-next");
				this.container.appendChild(next_frame);
				this.next_frame = next_frame;
				this.effect.setup(this.next_frame);
			}
			else if (next.type=='remote_html')
			{
				var callback = { 
					success: function(o) {
						var next_frame = document.createElement('div');
						next_frame.innerHTML = o.responseText;
						next_frame.setAttribute('id','frame_'+o.argument.id);
						o.argument.type='cached';
						o.argument.value=next_frame;
						YAHOO.util.Dom.addClass(next_frame, "yui-sldshw-frame");
						YAHOO.util.Dom.addClass(next_frame, "yui-sldshw-next");
						this.container.appendChild(next_frame);
						this.next_frame = o.argument.value;
						this.effect.setup(this.next_frame);
					},
					failure: function(o) {
						this.type='broken';
						this.choose_next_frame();
					},
					scope: this,
					argument: next
				}
				var transaction = YAHOO.util.Connect.asyncRequest('GET', next.value , callback,  null); 
			}
		},
	clean_up_transition : function() 
		{ 
			YAHOO.util.Dom.replaceClass(this.active_frame, "yui-sldshw-active", "yui-sldshw-cached");
			YAHOO.util.Dom.replaceClass(this.next_frame, "yui-sldshw-next", "yui-sldshw-active");
			this.active_frame = this.next_frame; 
		    this.choose_next_frame();
		},
	transition: function( o )
		{
		    var o = (o ==null) ? {} : o ;
 			var previous = (o.reverse ==null)?false:o.reverse;
			if (previous)
			{
			  this.choose_next_frame(true);
			}
            var hide = this.effect.get_animation(this.active_frame);
			
			hide.onComplete.subscribe(this.clean_up_transition, this, true);
		    hide.animate();
		}
	,
	loop: function()
		{
			var self;
			self =this;
			this.loop_interval = setInterval( function(){ self.transition();}, this.interval );
		}
 }	


YAHOO.MetaNET.slideshow.effects = {
	slideRight :{
			setup: function(frame){
				YAHOO.util.Dom.setStyle(frame, 'top', '0'); 
				YAHOO.util.Dom.setStyle(frame, 'left', '0'); 
			},
			get_animation: function(frame){
					var region = YAHOO.util.Dom.getRegion(frame);
					return new YAHOO.util.Motion(frame, { points: { by: [region.right-region.left,0] } }, 1, YAHOO.util.Easing.easeOut);
			}
		},
	slideLeft: {
			setup: function(frame){
					YAHOO.util.Dom.setStyle(frame, 'top', '0'); 
					YAHOO.util.Dom.setStyle(frame, 'left', '0'); 
			},
			get_animation: function(frame){
					var region = YAHOO.util.Dom.getRegion(frame);
					return new YAHOO.util.Motion(frame, { points: { by: [region.left-region.right,0] } }, 1, YAHOO.util.Easing.easeOut);
			}
		},
	squeezeLeft: {
			setup: function(frame){
					YAHOO.util.Dom.setStyle(frame, 'width', '100%'); 
			},
			get_animation: function(frame){
					var region = YAHOO.util.Dom.getRegion(frame);
					return new YAHOO.util.Anim(frame, { width: { to: 0 } }, 1, YAHOO.util.Easing.easeOut);
			}
		},
	squeezeRight: {
			setup: function(frame){
					YAHOO.util.Dom.setStyle(frame, 'width', '100%'); 
					YAHOO.util.Dom.setStyle(frame, 'right', '0px'); 
			},
			get_animation: function(frame){
					var region = YAHOO.util.Dom.getRegion(frame);
					YAHOO.util.Dom.setStyle(frame, 'right', '0px'); 
					return new YAHOO.util.Anim(frame, { width: { to: 0 }}, 1, YAHOO.util.Easing.easeOut);
			}
		},
	squeezeUp: {
			setup: function(frame){
					YAHOO.util.Dom.setStyle(frame, 'height', '100%'); 
			},
			get_animation: function(frame){
					var region = YAHOO.util.Dom.getRegion(frame);
					return new YAHOO.util.Anim(frame, { height: { to: 0 }}, 1, YAHOO.util.Easing.easeOut);
			}
		},
	squeezeDown: {
			setup: function(frame){
					YAHOO.util.Dom.setStyle(frame, 'height', '100%'); 
			},
			get_animation: function(frame){
					var region = YAHOO.util.Dom.getRegion(frame);
					YAHOO.util.Dom.setStyle(frame, 'bottom', '0px'); 
					return new YAHOO.util.Anim(frame, { height: { to: 0 }}, 1, YAHOO.util.Easing.easeOut);
			}
		},
	fadeOut: {
			setup: function(frame){
					YAHOO.util.Dom.setStyle(frame, 'opacity', '1'); 
			},
			get_animation: function(frame){
					return new YAHOO.util.Anim(frame, { opacity: { to: 0 }}, 1, YAHOO.util.Easing.easeOut);
			}
		},
	fadeIn: {
			setup: function(frame){
					YAHOO.util.Dom.setStyle(frame, 'opacity', '0'); 
					YAHOO.util.Dom.setStyle(frame, 'z-index', '20'); 
			},
			get_animation: function(frame){
					var region = YAHOO.util.Dom.getRegion(frame);
					return new YAHOO.util.Anim(frame, { opacity: { to: 1 }}, 1, YAHOO.util.Easing.easeOut);
			}
		}  
}
YAHOO.MetaNET.slideshow.effects.slideUp={
			setup: function(frame){
					YAHOO.util.Dom.setStyle(frame, 'top', '0'); 
					YAHOO.util.Dom.setStyle(frame, 'left', '0'); 
			},
			get_animation: function(frame){
					var region = YAHOO.util.Dom.getRegion(frame);
					return new YAHOO.util.Motion(frame, { points: { by: [0,region.top-region.bottom] } }, 1, YAHOO.util.Easing.easeOut);
			}
}
