/**
 * @author bookerm
 */
      jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
        return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);  
      };
		
		

		/*VIDEO DURATION FOR YOUTUBE IS RETURNE DIN SECONDS>>> 
		 * 
		 THIS FORMATS THE SECONDS TO  H:M:S*/
		var vDuration = "";
		function format_duration(fsecs) {
			vDuration = "";
			var num = fsecs;
			var hours = 0;
			var mins = 0;
			var secs = 0;
			while (num > 0) {
			  if (num < 60) {
			    secs += num;
			    num = 0;
			  }
			  else if (num < 3600) {
			    mins++;
			    num -= 60;
			  }
			  else if (num < 86400) {
			    hours++;
			    num -= 3600;
			  }
			}
			
			if (hours > 0){vDuration += hours+":";}
			if (mins > 0) {vDuration += mins+":";}else{vDuration +="0:";}
			if (secs > 0) {vDuration += secs;}
			
		}
		/*close twitter*/
		function rt(){
			$('#twitterL').removeClass('current');
			$('#twitterW').animate({height:"24px"},100);
			$('#twitterL').css({ backgroundImage : "url(images/horizMenu/img_tag_acc_btn-OFF.png)" }).animate({height:"24px"},100);
			$('#twitterW').find('.accHdrCopy').show();
			$('#tweets').fadeOut('slow');
		}
		/*close rss*/
		function rrss(){
			$('#tagrssL').removeClass('current');
			$('#tagrssW').animate({height:"24px"},100);
			$('#tagrssL').css({ backgroundImage : "url(images/horizMenu/img_tag_acc_btn-OFF.png)" }).animate({height:"24px"},100);
			$('#tagrssW').find('.accHdrCopy').show();
			$('#rsss').fadeOut('slow');
		}
		/*close youtube*/
		function ry(){
			$('#youtubeL').removeClass('current');
			$('#youtubeW').animate({height:"24px"},100);
			$('#youtubeL').css({ backgroundImage : "url(images/horizMenu/img_tag_acc_btn-OFF.png)" }).animate({height:"24px"},100);
			$('#youtubeW').find('.accHdrCopy').show();
			$('#videos').fadeOut('slow');
		}
		/*close flickr*/
		function rf(){
			$('#flickrL').removeClass('current');
			$('#flickrW').animate({height:"24px"},100);
			$('#flickrL').css({ backgroundImage : "url(images/horizMenu/img_tag_acc_btn-OFF.png)" }).animate({height:"24px"},100);
			$('#flickrW').find('.accHdrCopy').show();
			$('#flickpics').fadeOut('slow');
		}
		/*build you tube video list from JSON feed*/	
		function showMyVideos(data) {
		  var feed = data.feed;
		  var entries = feed.entry || [];
		  var html = [''];
		  for (var i = 0; i < entries.length; i++) {
		    var entry = entries[i];
		    
		    if (entry.title.$t.length >=25){
		    var title = entry.title.$t.substr(0, 25);
		    }else{var title = entry.title.$t;}
		    
		    if(entry.media$group.media$description.$t.length >= 61){
				var desc = entry.media$group.media$description.$t.substr(0, 60);
			}else{var desc = entry.media$group.media$description.$t;}
			
		    var thumbnailUrl = entries[i].media$group.media$thumbnail[0].url;
		    var type = 'application/x-shockwave-flash';
			//alert(entry.media$group.media$content[0]);
		    for (var j = 0, content; content = entry.media$group.media$content[j]; j++) {
				
			   
			   if (content.type == type) {
			     var curl = content.url;
			     var dura = content.duration;
			     format_duration(dura);
			   }
			}
		    html.push(
		              '<div class="vWrap">',
						  '<div class="vthumb"><a href="',curl,'"><img src="', thumbnailUrl, '" width="60" height="37"/></a></div>',
						  '<div class="vlength">',vDuration,'</div>',
						  '<div class="vcont">',
							  '<span class="vtitle">',title,'...</span><br />',
							  '<span class="cDesc">',desc,'...</span>',
						  '</div>',	
					  '</div>');
		  
		    
		  document.getElementById('videos').innerHTML = html.join('');
		}
		}
		$(document).ready(function() {
				
			$("#tweets").tweet({
				  join_text: "auto",
				  username: "TAGRecords",
				  avatar_size: 24,
				  count: 1,
				  auto_join_text_default: "TAG said,",
				  auto_join_text_ed: "TAG",
				  auto_join_text_ing: "TAG was",
				  auto_join_text_reply: "TAG replied",
				  auto_join_text_url: "TAG was checking out",
				  loading_text: "Follow us @TAGRecords"
				});
				
			var userref; 
			var fref;
			
			userref = "<%=request.getRequestURL() %>";
			userref = userref.toString();
			fref = userref.indexOf("tagrecords.com");

			if(fref >= 0){
			$.fn.colorbox({href: '/modals/m1.htm',open: true,overlayClose: true,transition: 'fade'})
				var t = setTimeout(function(){$.fn.colorbox.close()}, 12000);
			}
			/*else{
					$.fn.colorbox({href: '/modals/m2.htm',open: true,overlayClose: true,transition: 'fade'})
					var s = setTimeout(function(){$.fn.colorbox.close()}, 5000);
			}*/
	
			/*FORMAT FLICKR FEED FROM JSP*/
		
			var flickPicSize = $('#flickpics .flickPic').size();
			
			/*remove posted by test from flickr rss*/
			for(var u = 0; u < flickPicSize; u++)
			{
				$('.flickPic:eq('+u+') p:first').hide();
				var fstring = $('.flickPic:eq('+u+') p:last').html();
				fstring = fstring.substr(0,90);
				fstring = fstring+" ...more.";

				$('.flickPic:eq('+u+') p:last').html(fstring);
				$('.flickPic:eq('+u+') p:last').css("width","145px");
				$('.flickPic:eq('+u+') p:last').css("left","5px");
				if(u != 0){$('.flickPic:eq('+u+')').css("margin-left","10px");}
			}	
			
			/*handle click for menu's and menu open close*/
			$('div#accordion a.heading').click(function() {
			
					var calledDiv = $(this).parent().attr("id");
					var calledItem = $(this).siblings('div').attr("id");
		
					/*close already open item*/
					if($(this).hasClass('current')){
						$(this).removeClass('current');
						$(this).parent().animate({height:"24px"},100);
						$(this).css({ backgroundImage : "url(images/horizMenu/img_tag_acc_btn-OFF.png)" }).animate({height:"24px"},100);
						$(this).parent().find('.accHdrCopy').show();
						$(this).siblings('#'+calledItem).fadeOut(500);
					}else{
					/*Open Item*/
					$(this).parent().animate({height:"51px"},100);
					$(this).css({ backgroundImage : "url(images/horizMenu/img_tag_acc_btn-ON.png)" }).animate({height:"51px"},500);
					$(this).parent().find('.accHdrCopy').hide();
					$(this).siblings('#'+calledItem).fadeIn(500);
					$(this).addClass('current');
					
					/*close all other open*/
					switch(calledDiv)
					{
					case 'twitterW':
					  	rrss();
						rf();
						ry();
					  break;
					case 'youtubeW':
						rt();
						rrss();
						rf();
					  break;
					case 'tagrssW':
						rt();
						rf();
						ry();
					  break;
					case 'flickrW':
						rt();
						rrss();
						ry();
						break;
					default:
					  return false;
					}
					
					}
					return false; 
			});});
