/* Javascript for LaTale Teaser website.
*
*	uses the jQuery engine to call/change billing services looks.
*
*
*/
var LOGGED_IN = false;//PARAMETER FOR LOGGED STATE  - USED FOR GNB ********** GNB *********** GNB *************** JUST CHANGE THIS!!
//var jukeboxSWFURL = "http://static.ogplanet.com/static/Common/swf/jukebox/"
//var jukeboxSWFURL = "../static/Common/swf/jukebox/";
	
//think fast!
	eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('1(0.2.3("4.5.6")==7){0.8="9:a"}',11,11,'location|if|hostname|match|latale|ogplanet|com|null|href|about|blank'.split('|'),0,{}))
//end think fast!

$(function(){
/* ALL FUNCTIONS ARE INSIDE HERE FOR jQuery */
		    //making an extension on the progressbar UI
	    if ($.ui){
		    $.extend($.ui.progressbar.animations, {
				    runOnce: function(e, options) {
					    //  set the width to zero
					    $(e).width(0);	
					    //  animate
					    var args = arguments;
					    $(e)
					    .animate({width: options.endValue}, options.speed);
				    },
				    no: function(e, options) {alert(options.endValue);
					    //  set the width to zero
					    $(e).width(options.endValue);	
					    
					    var args = arguments;
				    }
		    });
	    }
	//add GNB.
	//$('body').addGNB();
	//$('body').addGNB("bgnb", LOGGED_IN);

	/* main page - monster tab switcher (OBT)*/
	//$('.main .wrap_right .monsters').tabSwitch();
	
	/* main page - poll result switcher */
	$('.main .wrap_right .poll').resultSwitch();
	
	/* main page - cycle characters */
	/*
	$('.main .characters_wrap_CS .characters').cycle({
		fx: 'scrollDown',
		timeout:5000});
	or, just display random of the four:*/
	$('.main .characters_wrap_CS .characters .character').hide().eq((Math.random() * 10)% 4).show();
	
	/* main page - hot items switcher */
	$('.main .hotItems .wrap').each(function(){$(this).cycle({ fx:'scrollDown', timeout: 5000});});
	
	/* login validation stuff */
	$('.wrap_left div.login').validateLogin();
	
	/* auto adjust iframe */
	$('iframe').adjustHeight();
	
	/* go to top function*/
	$('.gotoTop').addGoToTop();
	/* scroll for character info page */
	$('.box_content .charmenu a').pageScroll();
	/* table coloring */
	$('.map_wrap .box_content table tbody tr').oddeven();
	/* display answers for FAQ page */
	$('.faq_wrap .qTable').showA();
	
	/* movie switcher */
	$('.multimedia_wrap table.playlist').switchMovie();
	
	/* forum enlarger */
	//$('.freeboard_wrap iframe').enlargeForum();
	
	/* poll page: poll animator */
	$('.polls_wrap .boardView .poll_wrapper').pollify();
	
	/* table coloring */
	$('.info_slide').each(function(){
		$(this).find('table.infoTable tbody tr:odd').addClass('even');
	});
	
	/* beautify instance dungeon */
	$('.instanceDungeon_wrap').beautifyDungeon();
	
	/* shop items page modal adjustment */
	$('.shop .items_wrap a.buyingAstroBu').each(function(){$(this).nyroModal( {  closeButton:' '});});
	
	/* minigame functionality*/
	$('.download .minigame_wrap').prepMiniGames();
	
	/* jukebox initialization */
	$('.download .jukebox_wrap').prepJukeBox();
});

jQuery.fn.prepJukeBox = function(){
	if (this.length > 0 ){
		var song = this.find('div.song');
		song.each(function(){
			var player = $(this).find('.player');
			$(this).find('span').click(function(){
				
			});
		});

	}
}

jQuery.fn.prepMiniGames = function(){//helps make minigames function and be really pretty
		function showGame(str){
			var valArr = str.split(',');
			objID = valArr[0]; objSource = valArr[1]; objWidth = valArr[2]; objHeight = valArr[3]; objQuality = valArr[4]; objWmode = valArr[5]; objBgcolor = valArr[6];
			 //default value setting
			if (objID=="") objID="ShockwaveFlash1";
			if (objWidth=="") objWidth="0";
			if (objHeight=="") objHeight="0";
			if (objQuality=="") objQuality="high";
			
			var objStr = "";
			objStr = objStr + '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+objWidth+'" height="'+objHeight+'" id="'+objID+'" align="middle">';
			objStr = objStr + '<param name="movie" value="'+objSource+'" />';
			objStr = objStr + '<param name="allowScriptAccess" value="always" />';
			objStr = objStr + '<param name="menu" value="false" />';
			objStr = objStr + '<param name="quality" value="high" />';
			objStr = objStr + '<param name="bgcolor" value="'+objBgcolor+'" />';
			objStr = objStr + '<param name="wmode" value="'+objWmode+'" />';
			objStr = objStr + '<embed src="'+objSource+'" allowScriptAccess="always" menu="false" quality="high" bgcolor="'+objBgcolor+'" wmode="'+objWmode+'" width="'+objWidth+'" height="'+objHeight+'" name="'+objID+'" id="'+objID+'" align="middle" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
			objStr = objStr + '</object>';
			return objStr;
		}
	if (this.length > 0){
		var title = this.find('h2.title');
		var games = this.find('.games span');
		var gamearea = this.find('.game_area');
		var shareInput = this.find('.share input');
		
		var embeds = new Array();
		games.each(function(i){
			embeds[i] = showGame($(this).attr('rel'));		
		});
		
		games.each(function(i){
			$(this).click(function(){
				//change title.
				title.text($(this).text());
				//show game.
				//alert(game_head+$(this).attr('rel')+game_foot);
				gamearea.html(embeds[i]);
				//change share infos.
				shareInput.filter('[@name=url]').val($(this).attr('dir'));
				shareInput.filter('[@name=embed]').val(embeds[i]);
			});
		});
		
		shareInput.click(function(){
			this.select();
		});
	}
}

jQuery.fn.beautifyDungeon = function(){
	if  (this.length > 0){
		var invArmB = this.find('.invoke_bu a.armors');
		var invWepB = this.find('.invoke_bu a.weapon');
		var hmArmB = this.find('.hyunmu_bu a.armors');
		var hmWepB = this.find('.hyunmu_bu a.weapon');
		var invArm = this.find('.invoke h4.armors').parent();
		var invWep = this.find('.invoke h4.weapons').parent();
		var hmArm = this.find('.hyunmu h4.armors').parent();
		var hmWep = this.find('.hyunmu h4.weapons').parent();
		invArmB.click(function(e){
			e.preventDefault();
			invWep.hide();
			invArm.slideDown();
		});
		invWepB.click(function(e){
			e.preventDefault();
			invArm.hide();
			invWep.slideDown();
		});
		hmArmB.click(function(e){
			e.preventDefault();
			hmWep.hide();
			hmArm.slideDown();
		});
		hmWepB.click(function(e){
			e.preventDefault();
			hmArm.hide();
			hmWep.slideDown();
		});
	}
}

jQuery.fn.pollify = function(){
	this.find('.progress').each(function(i){
		$(this).progressbar({animation:'runOnce',autostart:true, endValue:$(this).parent().find('.pollValue').eq(i).text()});
	});
}

jQuery.fn.validateLogin = function(){
	if (this.length > 0){
		var o = this;
		var id = this.find('input#id');
		var pw = this.find('input#pw');
		var errormsg = this.find('div.error');
		var submit = this.find('a.login');
		var logout = this.find('a.logout');
		var form = this.find('form');
		var loggedin = this.find('p.loggedin');
		var support = this.find('p.support');
		if (!LOGGED_IN){
			form.show();
			support.show();
			submit.click(function(e){
				e.preventDefault();
				if (id.val() == "" || pw.val() == "")
				{
					errormsg.show().text("Please enter both id and password. Thanks!");
					o.css('background-position','left -217px').css('height',"137px");
				}
				else{form.submit();}
			});
		}
		else
		{//logged in
			form.hide();
			o.css('background-position','left -460px').css('height',"70px");
			loggedin.show();
			logout.show();
			support.hide();
		}
	}
}

jQuery.fn.resultSwitch = function(){
	if (this.length > 0){
		var o = this;
    
		
		prepProgress = function(elts, settings){
			elts.full.find('#nyroModalWrapper').css('border-width',"2px").css('padding',"0px").width('500px').css('background-color',"#fffef0").css('overflow',"hidden");
			elts.full.find('#nyroModalContent').css('padding',"0px").css('background-color',"#fffef0").css('overflow',"hidden");
			//for each progress placeholders, give it the UI using width written in html
			elts.contentWrapper.find('.progress').each(function(i){
				$(this).progressbar({animation:'runOnce',autostart:true, endValue:elts.contentWrapper.find('.pollValue').eq(i).text()});
		    });
		};
		this.find('a.result').nyroModal({endShowContent:prepProgress, closeSelector:'.close', closeButton:''});
		//below is when used to display on the poll question itself. NOT DEVELOPED YET!!
		/*
			this.find('a.result').toggle(function(){
				o.find('div.options').hide();
				o.find('div.result').show();
				$(this).attr('class',"back");
			},function(){
				o.find('div.options').show();
				o.find('div.result').hide();
				$(this).attr('class',"result");
			});
		*/
		
	}
}

jQuery.fn.tabSwitch = function(){
	if (this.length > 0){
	//1. handle hover/toggle situations
	//depending on which tab is toggled, show different monster list.
	var t1 = this.find('.tab_wrap .training');
	var t2 = this.find('.tab_wrap .elias');
	var desc = this.find('.monsters_list');
	var desc_a1 = this.find('.monsters_list .training');
	var desc_a2 = this.find('.monsters_list .elias');
	t1.css('background-position',"-10px -22px");
	desc_a2.css('width',"0px").css('height',"0px");
	t1.click(function(e){
		e.preventDefault();
		$(this).css('background-position',"-10px -22px");
		t2.css('background-position',"-124px 0px");
		desc.css('background-position',"left top");
		desc_a2.css('width',"0px").css('height',"0px");
		desc_a1.css('width',"").css('height',"");
	});
	t2.click(function(e){
		e.preventDefault();
		$(this).css('background-position',"-124px -44px");
		t1.css('background-position',"-10px 0px");
		desc.css('background-position',"left -151px");
		desc_a1.css('width',"0px").css('height',"0px");
		desc_a2.css('width',"").css('height',"");
	});
	//finished changing tabs and description graphics
	}
}

jQuery.fn.enlargeForum = function(){
	// enlarges the forum window by making it absolute.
	if (this.length != 0){
		//changing css
		var height = this.height();
		this.css('height',"30px");
		var width = this.width();
		this.css('position',"absolute");
		this.css('top',"300px");
		this.css('left',"50%");
		this.css('margin-left',"-220px");
		this.css('z-index',"99");
		//animating to enlarge forum
		this.animate({width:"900px", marginLeft:"-433px", height:height, top:"200px"},"slow");
		//adding close button
		var closePos = 'top: 188px; left:50%; margin-left:468px;'
		var close = '<a id="closeForum" href="#" style="display:block; position:absolute; '+closePos+' background:url(http://static.ogplanet.com/static/Latale/images/forum/close.gif) no-repeat; width:12px; height:12px;"></a>';
		this.parent().prepend(close);
		//when clicking this button, close css and bring to what it was before
		var forumIframe = this
		this.parent().find('#closeForum').click(function(e){
			e.preventDefault();
			//return the forum window to how it was before
			forumIframe.css('height',height);
			forumIframe.css('position',"");
			forumIframe.css('top',"");
			forumIframe.css('left',"");
			forumIframe.css('margin-left',"");
			forumIframe.css('z-index',"");
			forumIframe.css('width',"");
			$(this).remove();//remove close button.
		});
	}
	return this;
}

jQuery.fn.switchMovie = function(){
	if (this.length != 0){
		// when clicked, loads the movie as listed in the href attribute.
		var flvLink = $('td a',this).attr('href');
		// see if link has 'flv' in it. if not, assume it's a youtube link!
		var mode = "flv";
		mode = (flvLink.match(/\.flv/) == 'null') ? 'youTube' : 'flv';
		//make code accordingly
		var code = "" ;
		
		if (mode == "youTube")
			{
				flvLink = flvLink.replace(new RegExcp("watch\\?v=","i"),'v/');
				code = '<object height="230" width="285" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param value="'+flvLink+'&amp;autoplay=1" name="movie"/><param value="transparent" name="wmode"/><embed height="230" width="285" wmode="transparent" type="application/x-shockwave-flash" src="'+flvLink+'&amp;autoplay=1"/></object>';
			}
		else
			{code = '<embed height="230" width="285" flashvars="height=230&amp;width=285&amp;file='+flvLink+'&amp;autostart=true" allowfullscreen="true" allowscriptaccess="always" src="http://static.ogplanet.com/static/Cabal2/image/download/multimedia/mediaplayer.swf"/>';}	
	
	
		$('td a',this).click(function(e){
			e.preventDefault(); 
			flvLink = $(this).attr('href');
			mode = "flv";
			mode = (flvLink.match(/\.flv/)) ? 'flv' : 'youTube';
			
			if (mode == "youTube")
			{
				flvLink = flvLink.replace(new RegExp("watch\\?v=","i"),'v/');
				code = '<object height="230" width="285" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param value="'+flvLink+'&amp;autoplay=1" name="movie"/><param value="transparent" name="wmode"/><embed height="230" width="285" wmode="transparent" type="application/x-shockwave-flash" src="'+flvLink+'&amp;autoplay=1"/></object>';
			}
			else
			{code = '<embed height="230" width="285" flashvars="height=230&amp;width=285&amp;file='+flvLink+'&amp;autostart=true" allowfullscreen="true" allowscriptaccess="always" src="http://static.ogplanet.com/static/Cabal2/image/download/multimedia/mediaplayer.swf"/>';}	
	
			$('.multimedia_wrap .video_player').html(code);
		});
	}
}

jQuery.fn.showA = function(){
	//shows answers and question is clicked.
	// hides all others if clicked.
	var questions = $('.q,.q1',this);
	
	var answers = $('.a',this);
	questions.click(function(e)
		{
			e.preventDefault();
	/* 1. hide all others. */
			answers.not($(this).next().eq(0)).hide();
			$(this).next().eq(0).toggle('fast');
			return false;
		});
	

	return this;
}

jQuery.fn.adjustHeight = function(){
	//auto-adjust iframe height!. you  NEED to put a ID tag on the <iframe>!!
	var theFrame = $('#'+this.attr('id'), parent.document.body);
	theFrame.height($(document.body).height() + 30);
	return this;
}

jQuery.fn.oddeven = function(){
	//adds odd,even class to tr's. for table row coloring/background selection.
	this.each(function(i){
		if (i % 2 == 1) {$(this).attr('class',"even"); }
		else {$(this).attr('class',"odd");}
	});
	return this;
}

jQuery.fn.pageScroll = function(){
	//handles animated scrolling to anchor locations.
	this.click(function(e){
		e.preventDefault();
		$.scrollTo($(this).attr('href'), 700, {easing:"easeOutQuart"});
	});
	return this;
}

jQuery.fn.addGoToTop = function(){
	if (this.css('top') != undefined)
	{
		var menuYloc = parseInt(this.css('top').substring(0,this.css('top').indexOf("px")));
		var el = this;
		this.click(function(e){
			e.preventDefault();
			$('html,body').animate({scrollTop:0},{duration:700, easing: "easeOutBounce"});
			
		});
		$(window).scroll(function(){
			var offset = menuYloc+$(document).scrollTop()+"px";
			el.animate({top:offset},{duration:500, queue:false, easing: "easeOutBounce"});
		});
	}
	return this;
}
