/**
 *  jquery.popupt
 *  (c) 2008 Semooh (http://semooh.jp/)
 *
 *  Dual licensed under the MIT (MIT-LICENSE.txt)
 *  and GPL (GPL-LICENSE.txt) licenses.
 *
 **/
(function($){
	$.fn.extend({
		imghover: function(opt){
			return this.each(function() {
        opt = $.extend({
            prefix: '',
            suffix: '_o',
            src: '',
            btnOnly: true,
            fade: false,
            fadeSpeed: 500
          }, opt || {});

        var node = $(this);
				if(!node.is('img')&&!node.is(':image')){
          var sel = 'img,:image';
          if (opt.btnOnly) sel = 'a '+sel;
          node.find(sel).imghover(opt);
          return;
        }

        var orgImg = node.attr('src');

        var hoverImg;
        if(opt.src){
          hoverImg = opt.src;
        }else{
          hoverImg = orgImg;
          if(opt.prefix){
            var pos = hoverImg.lastIndexOf('/');
            if(pos>0){
              hoverImg = hoverImg.substr(0,pos-1)+opt.prefix+hoverImg.substr(pos-1);
            }else{
              hoverImg = opt.prefix+hoverImg;
            }
          }
          if(opt.suffix){
            var pos = hoverImg.lastIndexOf('.');
            if(pos>0){
              hoverImg = hoverImg.substr(0,pos)+opt.suffix+hoverImg.substr(pos);
            }else{
              hoverImg = hoverImg+opt.suffix;
            }
          }
        }

        if(opt.fade){
          var offset = node.offset();
          var hover = node.clone(true);
          hover.attr('src', hoverImg);
          hover.css({
            position: 'absolute',
            left: offset.left,
            top: offset.top,
            zIndex: 1000
          }).hide().insertAfter(node);
          node.mouseover(
            function(){
              var offset=node.offset();
              hover.css({left: offset.left, top: offset.top});
              hover.fadeIn(opt.fadeSpeed);
              node.fadeOut(opt.fadeSpeed,function(){node.show()});
            }
          );
          hover.mouseout(
            function(){
              node.fadeIn(opt.fadeSpeed);
              hover.fadeOut(opt.fadeSpeed);
            }
          );
        }else{
          node.hover(
            function(){node.attr('src', hoverImg)},
            function(){node.attr('src', orgImg)}
          );
        }
			});
		}
	});
})(jQuery);


/*
 * 	Easy Slider 1.7 - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
/*
 *	markup example for $("#slider").easySlider();
 *	
 * 	<div id="slider">
 *		<ul>
 *			<li><img src="images/01.jpg" alt="" /></li>
 *			<li><img src="images/02.jpg" alt="" /></li>
 *			<li><img src="images/03.jpg" alt="" /></li>
 *			<li><img src="images/04.jpg" alt="" /></li>
 *			<li><img src="images/05.jpg" alt="" /></li>
 *		</ul>
 *	</div>
 *
 */

(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			controlsShow:	true,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	true,
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		false,
			speed: 			800,
			auto:			false,
			pause:			2000,
			continuous:		false, 
			numeric: 		false,
			numericId: 		'controls'
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this); 				
			var s = $("li", obj).length;
			var w = $("li", obj).width(); 
			var h = $("li", obj).height(); 
			var clickable = true;
			obj.width(w); 
			obj.height(h); 
			obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			$("ul", obj).css('width',s*w);			
			
			if(options.continuous){
				$("ul", obj).prepend($("ul li:last-child", obj).clone().css("margin-left","-"+ w +"px"));
				$("ul", obj).append($("ul li:nth-child(2)", obj).clone());
				$("ul", obj).css('width',(s+1)*w);
			};				
			
			if(!options.vertical) $("li", obj).css('float','left');
								
			if(options.controlsShow){
				var html = options.controlsBefore;				
				if(options.numeric){
					html += '<ol id="'+ options.numericId +'"></ol>';
				} else {
					if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
					html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
					html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
					if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';				
				};
				
				html += options.controlsAfter;						
				$(obj).after(html);										
			};
			
			if(options.numeric){									
				for(var i=0;i<s;i++){						
					$(document.createElement("li"))
						.attr('id',options.numericId + (i+1))
						.html('<a rel='+ i +' href=\"javascript:void(0);\">'+ (i+1) +'</a>')
						.appendTo($("#"+ options.numericId))
						.click(function(){							
							animate($("a",$(this)).attr('rel'),true);
						}); 												
				};							
			} else {
				$("a","#"+options.nextId).click(function(){		
					animate("next",true);
				});
				$("a","#"+options.prevId).click(function(){		
					animate("prev",true);				
				});	
				$("a","#"+options.firstId).click(function(){		
					animate("first",true);
				});				
				$("a","#"+options.lastId).click(function(){		
					animate("last",true);				
				});				
			};
			
			function setCurrent(i){
				i = parseInt(i)+1;
				$("li", "#" + options.numericId).removeClass("current");
				$("li#" + options.numericId + i).addClass("current");
			};
			
			function adjust(){
				if(t>ts) t=0;		
				if(t<0) t=ts;	
				if(!options.vertical) {
					$("ul",obj).css("margin-left",(t*w*-1));
				} else {
					$("ul",obj).css("margin-left",(t*h*-1));
				}
				clickable = true;
				if(options.numeric) setCurrent(t);
			};
			
			function animate(dir,clicked){
				if (clickable){
					clickable = false;
					var ot = t;				
					switch(dir){
						case "next":
							t = (ot>=ts) ? (options.continuous ? t+1 : ts) : t+1;						
							break; 
						case "prev":
							t = (t<=0) ? (options.continuous ? t-1 : 0) : t-1;
							break; 
						case "first":
							t = 0;
							break; 
						case "last":
							t = ts;
							break; 
						default:
							t = dir;
							break; 
					};	
					var diff = Math.abs(ot-t);
					var speed = diff*options.speed;		

					if(!options.vertical) {
						p = (t*w*-1);
						$("ul",obj).animate(
							{ marginLeft: p }, 
							{ queue:false, duration:0,complete:adjust }
						);				
					} else {
						p = (t*h*-1);
						$("ul",obj).animate(
							{ marginTop: p }, 
							{ queue:false, duration:0, "easing": "linear",complete:adjust }
						);					
					};
					
					if(!options.continuous && options.controlsFade){					
						if(t==ts){
							$("a","#"+options.nextId).hide();
							$("a","#"+options.lastId).hide();
						} else {
							$("a","#"+options.nextId).show();
							$("a","#"+options.lastId).show();					
						};
						if(t==0){
							$("a","#"+options.prevId).hide();
							$("a","#"+options.firstId).hide();
						} else {
							$("a","#"+options.prevId).show();
							$("a","#"+options.firstId).show();
						};					
					};				
					
					if(clicked) clearTimeout(timeout);
					if(options.auto && dir=="next" && !clicked){;
						timeout = setTimeout(function(){
							animate("next",false);
						},diff*options.speed+options.pause);
					};
			
				};
				
			};
			// init
			var timeout;
			if(options.auto){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};		
			
			if(options.numeric) setCurrent(0);
		
			if(!options.continuous && options.controlsFade){					
				$("a","#"+options.prevId).hide();
				$("a","#"+options.firstId).hide();				
			};				
			
		});
	  
	};

})(jQuery);

/*************************/
/* listen/ soundclip page javascript  */


var KEY_NULL = null;
var KEY_NONE = 0;
var KEY_BCKSPC = 8;
var KEY_TAB = 9;
var KEY_ENTER = 13;
var KEY_ESC = 27;
var KEY_SPACE = 32;

function getSliderText(sValue,eValue)
{
	var sm  = parseInt(sValue / 60);
	var ss  = sValue - (parseInt(sValue / 60) * 60 );
	var em  = parseInt(eValue / 60);
	var es  = eValue - (parseInt(eValue / 60) * 60 );
	if (sm < 10 )
	{
		sm = '0' + sm;
	}
	if (ss < 10 )
	{
		ss = '0' + ss;
	}
	if (em < 10 )
	{
		em = '0' + em;
	}
	if (es < 10 )
	{
		es = '0' + es;
	}
	return 'From <span>'  + sm + ':'  + ss  +  '</span> - To <span>' + em  + ':'  + es + '</span>';
}
function loadAudioContent(eid,id)
{
	var start = jQuery("#slider").slider("values", 0);
	var end = jQuery("#slider").slider("values", 1);
	var timestamp_id = new Date().getTime();
	jQuery("#audiocontent").load( live_site_url + "index.php?option=com_rsevents&task=showmp3flashcontent&controller=events", {cid : eid,fstart:start,fend:end,timestamp :timestamp_id},function(responseText, textStatus, XMLHttpRequest){
			 });
}

function expandoption(ele)
{
	if (jQuery(ele).parent().children(".optiondetails").is(":hidden"))
	{
		jQuery(".mp3SoundClip .optionlist .optiondetails").slideUp();
		jQuery(ele).parent().children(".optiondetails").slideDown();
	}
}
function clipsoundpart(eid)
{
	/*
	var start = jQuery("#slider").slider("values", 0);
	var end = jQuery("#slider").slider("values", 1); */


	var start = jQuery("#starttime").val();
	var end = jQuery("#endtime").val();
	var timestamp_id = new Date().getTime();
	jQuery("#audiocontent").load( live_site_url + "index.php?option=com_rsevents&task=showmp3flashcontent&controller=events", {cid : eid,fstart:start,fend:end,timestamp :timestamp_id},function(responseText, textStatus, XMLHttpRequest){ 
				if (textStatus == "success")
				{
					jQuery(".optioncontent").slideDown();
					jQuery("#hstart").val(start);
					jQuery("#hend").val(end);
				}
			 });
}
function doOption1(eid)
{
	var start = jQuery("#hstart").val();
	var end  = jQuery("#hend").val();
	var timestamp_id = new Date().getTime();
	var comment = jQuery("#opt1comment").val();

	jQuery.get( live_site_url + "index.php?option=com_rsevents&task=savesoundbyte&controller=events", {cid : eid,fstart:start,fend:end,timestamp :timestamp_id,fcomment:comment},function(responseText, textStatus, XMLHttpRequest){ 
		if (textStatus == "success")
		{
			if (responseText == "error")
			{
				location.replace( live_site_url + 'index.php?option=com_user&view=login&Itemid=137');
			}
			else
			{
				showSoundClipMsg("Download link successfully sent to user email ID.");
			}
		}
	 });
}

function doOption2(eid)
{

	var start = jQuery("#hstart").val();
	var end  = jQuery("#hend").val();
	var timestamp_id = new Date().getTime();
	var comment = jQuery("#opt2comment").val();

	jQuery.get( live_site_url + "index.php?option=com_rsevents&task=sendsoundbytemyself&controller=events", {cid : eid,fstart:start,fend:end,timestamp :timestamp_id,fcomment:comment},function(responseText, textStatus, XMLHttpRequest){ 
		if (textStatus == "success")
		{
			if (responseText == "error")
			{
				location.replace( live_site_url + 'index.php?option=com_user&view=login&Itemid=137');
			}
			else
			{
				showSoundClipMsg("Sound Clip link successfully sent to user email ID.");
			}
		}
	 });
}

function doOption3(eid)
{

	var start = jQuery("#hstart").val();
	var end  = jQuery("#hend").val();
	var timestamp_id = new Date().getTime();
	var comment = jQuery("#opt3comment").val();
	var email = jQuery("#opt3email").val();

	jQuery.get( live_site_url + "index.php?option=com_rsevents&task=sendsoundbytefriend&controller=events", {cid : eid,fstart:start,fend:end,timestamp :timestamp_id,femail:email,fcomment:comment},function(responseText, textStatus, XMLHttpRequest){ 
		if (textStatus == "success")
		{
			if (responseText == "error")
			{
				location.replace( live_site_url + 'index.php?option=com_user&view=login&Itemid=137');
			}
			else
			{
				showSoundClipMsg("Sound Clip link successfully sent to your friend's email ID.");
			}
		}
	 });
}

function showSoundClipMsg(msgtext)
{
	jQuery("#soundclipmsg").html(msgtext);
		jQuery("#soundclipmsg").fadeIn(2000,function() {
				jQuery("#soundclipmsg").fadeOut(2000);
		  });		
}

function numbersOnly(e,ele,zeroValueAllow)
{
	var key;
	var keychar;
	
	if (window.event) 
	{
		key = window.event.keyCode;
	}
	else if (e) 
	{
		key = e.which;
	}
	else
	{
		return true;
	}

	keychar = String.fromCharCode(key);

	// for Ctrl + R  		
	if (e.ctrlKey && ( keychar == 'R' || keychar == 'r' ))
	{
		return true;
	}
	// Control keys (no @#$% "magic numbers")
	if ((key == KEY_NULL) ||  (key == KEY_NONE) || (key == KEY_BCKSPC) || (key == KEY_TAB) || (key == KEY_ENTER) || (key == KEY_ESC) )
	 {
		return true;
	 }
	else 
	{ 
		var val =  document.getElementById(ele).value;
		if (zeroValueAllow)
		{
			if ( val.length == 0 )
			{
				if (keychar == '0' )
					return true;

			}
			if (("0123456789").indexOf(keychar) > -1 )
			{
				return true;
			}
		}
		else
		{
			if (keychar == '0' )
			{		
					if ( val.length == 0 )
						return false;	
					else
						return true;
			}
			else if (("123456789").indexOf(keychar) > -1 )
			{
				return true;
			}
		}
		
	}
	return false;
}
function putText(ele,zeroValueAllow)
{
	 var val =  document.getElementById(ele).value;
	 var newval = val;
	 
	//newval =  ltrim(val,'0');
	if (zeroValueAllow)
	{

		 if (val == 0 || val == '')
			newval = '0';
		 else
			newval =  ltrim(val,'0');
	}
	else
	{
		newval =  ltrim(val,'0');
	}

	document.getElementById(ele).value = newval;	
	
}
function trim(str, chars) 
{
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) 
{
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) 
{
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


/****************************************/
/* Javascript to add event to Add to Favourites */

function showAddtoFavorite(eid)
{
	jQuery.get( live_site_url + "isValidUser.php",{},
		function(data){
			    result = jQuery(data).find('result').text();
			    if (result == 'error')
					{
						var ajaxCall = 'jax.call("community", "system,ajaxLoginRegister","' +  window.location.href + '")';
						cWindowShow(ajaxCall, '', 450, 100);
					}
			    else 
					{
			    		var ajaxCall = 'jax.call("addfavourites","","showform",' + eid + ')';
						cWindowShow(ajaxCall, 'Add To Favorite', 200, 100);	
					}
			  },'xml');
}

function addToFavourites(EventID)
{
	jQuery('input[name=fcategoryID]:radio').each(function(){
			if (jQuery(this).attr('checked'))
				categoryID = jQuery(this).val(); 
	});


	var timestamp_id = new Date().getTime();	
	jQuery.get( live_site_url + "index2.php?option=com_addfavourites&task=add", { categoryID: categoryID, arg2: EventID , timestamp :timestamp_id},
			  function(data){
			    result = jQuery(data).find('result').text();
			    if (result == 'error')
			    	window.location= live_site_url + "index.php?option=com_user&view=login";
			    else 
			    	var ajaxCall = 'jax.call("addfavourites","","successform")';
					cWindowShow(ajaxCall, 'Successfully Added', 400, 100);	
			  },'xml');
}
 


function showdelFavorite(id,cid)
{

	var ajaxCall = 'jax.call("addfavourites","","showDelform",' + id + ',' + cid + ')';
	cWindowShow(ajaxCall, 'Delete Favorite', 200, 80);	
}

function DeleteFavourites(EventID,categoryID)
{
	jQuery('input[name=deleteFav]:radio').each(function(){
			if (jQuery(this).attr('checked'))
				delVal = jQuery(this).val(); 
	});

 	categoryID = categoryID;
	EventID = EventID;

	if(delVal==true) {
		jQuery.get( live_site_url + "index.php?option=com_addfavourites&task=delete", { arg5: categoryID, arg6: EventID},
				  function(data){
					result = jQuery(data).find('delresult').text();
					if (result == 'error')
						window.location= live_site_url + "index.php?option=com_addfavourites&task=list";
					else 
						var ajaxCall = 'jax.call("addfavourites","","successDelform")';
						cWindowShow(ajaxCall, 'Successfully Deleted', 350, 50);	
						
						
				  },'xml');
	   setTimeout('cWindowRefresh()',3500);
	}  else {
	
	}
}

function cWindowRefresh() {
	window.location= live_site_url + "index.php?option=com_addfavourites&task=list";
}


/*****************************************/
/* javascript to show report and issue form */

function callReportIssueForm()
{
	var ajaxCall = 'jax.call("reportanissue","","showform","' +  document.location.href +'")';
	cWindowShow(ajaxCall, 'Report Issue', 500, 320,'error');	
}


function checkReportIssueForm()
{
    //name validation
	if( jQuery( '#report-form #name' ).val() == '' )
	{
		jQuery( '#report-form #report-message-error' ).html( 'Name&nbsp;cannot&nbsp;be&nbsp;left&nbsp;empty ' ).css( 'color' , 'red' );
		return false;		
	}

	//email	validation
	if( jQuery( '#report-form #email' ).val() == '' )
	{
		jQuery( '#report-form #report-message-error' ).html( 'Email&nbsp;cannot&nbsp;be&nbsp;left&nbsp;empty ' ).css( 'color' , 'red' );
		return false;		
	} else {
		var email =  jQuery( '#report-form #email' ).val();
		var re = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
		var email = email.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');

		if (!re.test(email)) {
			jQuery( '#report-form #report-message-error' ).html( 'Enter&nbsp;valid&nbsp;email ' ).css( 'color' , 'red' );
			return false;
		}

	}

    //name validation
	if( jQuery( '#report-form #subject' ).val() == '' )
	{
		jQuery( '#report-form #report-message-error' ).html( 'Subject&nbsp;cannot&nbsp;be&nbsp;left&nbsp;empty ' ).css( 'color' , 'red' );
		return false;		
	}

	//Commets validation
	if( jQuery( '#report-form #report-message' ).val() == '' )
	{
		jQuery( '#report-form #report-message-error' ).html( 'Tell&nbsp;us&nbsp;about&nbsp;the&nbsp;problem&nbsp;you&nbsp;encountered&nbsp;on&nbsp;this&nbsp;web&nbsp;site' ).css( 'color' , 'red' );
		return false;		
	}
	
	return true;
}

function submitReportIssueForm()
{
	if ( checkReportIssueForm())
	{
		jQuery.post( live_site_url + 'index.php', jQuery("#report-form").serialize() ,function(data) {
			var ajaxcall='jax.call("reportanissue", "","showsaveform")';
			cWindowShow(ajaxcall, 'Report Sent', 450, 100);
		});
	}
}


/* on load javascript functions */

jQuery(document).ready(function(){	
			
	/* for images effect*/
	jQuery('.image-hover').imghover({suffix: '-hovered'});
	jQuery('.text-image-hover').imghover({suffix: '-hovered'});
	jQuery('.text-image-hover').mouseover(function(){
		jQuery(this).parent().parent().next().css("color","#8D0000");
	});
	jQuery('.text-image-hover').mouseout(function(){
		jQuery(this).parent().parent().next().css("color","#000");
	});


	jQuery(".slider").easySlider({ 
		auto: true, 
		continuous: true,
		speed:4000,
		controlsShow:false
	});
	
	 jQuery('.tooltip-head .toottip-point').each(function(){ 
	 var content = jQuery(this).next('.tooltip-content').html();
	 jQuery(this).tooltip(
			{ 
				bodyHandler: function() { 
					return content; 
				 },	
				showURL: false 
				
			});
	 });

});	


/* functions to show popup */
function PopupCenter(pageURL, title,w,h,loginRequired) 
{
	if (typeof loginRequired == 'undefined' ) loginRequired = false;
	if (loginRequired)
	{
		jQuery.get( live_site_url + "isValidUser.php",{},
		function(data){
				result = jQuery(data).find('result').text();
				if (result == 'error')
					{
						var ajaxCall = 'jax.call("community", "system,ajaxLoginRegister","' +  window.location.href + '")';
						cWindowShow(ajaxCall, '', 450, 100);
					}
				else 
					{
						showpopup(pageURL, title,w,h);
					}
			  },'xml');
	}
	else
	{
		showpopup(pageURL, title,w,h);
	}
}
function showpopup(pageURL, title,w,h)
{
	var left = (screen.width/2)-(w/2);
	var top = (screen.height/2)-(h/2);
	
	window.open(pageURL,'_blank','height=' + h + ',width=' + w + 'top=' + top + ',left=' + left +  ',fullscreen:No,center:Yes,help:No,resizable:No,status:No,scrollbars=1',true);
}


/* for float menu more */

jQuery(function() {
	var $oe_menu		= jQuery('.red_menu_containter');
	var $oe_menu_items	= jQuery('.moremenucontainer');
	
	$oe_menu_items.bind('mouseenter',function(){
		
		var $this = jQuery(this);
		$this.addClass('slided selected');
		//$this.children('div.moremenu').css( 'left' ,  $this.position().left -  ( $this.children('div.moremenu').width() / 2 ) ) ;
		$this.children('div.moremenu').css('z-index','9999').stop(true,true).slideDown(500,function(){
			$oe_menu_items.not('.slided').children('div.moremenu').hide();
			$this.removeClass('slided');
		
		});
	}).bind('mouseleave',function(){
		var $this = jQuery(this);
		$this.removeClass('selected').children('div.moremenu').css('z-index','1');
	});
	
	$oe_menu.bind('mouseenter',function(){
		var $this = jQuery(this);
		$this.addClass('hovered');
	}).bind('mouseleave',function(){
		var $this = jQuery(this);
		$this.removeClass('hovered');
		$oe_menu_items.children('div.moremenu').hide();
	})

});
