// JavaScript Document
var inviteDialog;

$(document).ready(function() {

	
	$('.hover').bind("mouseenter",function(){
		$(this).animate({ opacity:0.5},500).animate({ opacity:1},500);								   
	}).bind("mouseleave",function(){
		$(this).animate({ opacity:1},500);								   
	});

	
	$('#header-quotes').innerfade({ speed: 'slow', timeout: 4000, type: 'sequence', containerheight: '220px' }); 
	
	$('#main-nav li').bind('mouseenter',function(){
			$(this).addClass('over');					 
	}).bind('mouseleave',function(){
			$(this).removeClass('over');
	});
	
	
	$('#nav li').hover(
		function () {
			
			$(this).closest('ul.top > li').addClass('over');	
			//show its submenu
			$('ul', this).slideDown(100);

		}, 
		function () {
			//hide its submenu
			$(this).removeClass('over');
			$('ul', this).slideUp(100);			
		}
	);
	
	findimg();
	
	
	$(".youtube").colorbox({iframe:true, innerWidth:640, innerHeight:390});
});


function findimg()
{
 var imgs,i;
// loop through all images of the document
 imgs=document.getElementsByTagName('img');
 for(i=0;i<imgs.length;i++)
 {
// test if the class 'roll' exists
  if(/roll/.test(imgs[i].className))
  {
// add the function roll to the image onmouseover and onmouseout and send
// the image itself as an object
   imgs[i].onmouseover=function(){roll(this);};
   imgs[i].onmouseout=function(){roll(this);};
  }
 }
}


function roll(o){
 var src,ftype,newsrc;
 // get the src of the image, and find out the file extension
 
 if (!$(o).parent().parent().hasClass('selected_character')){	
		 src = o.src;
		 ftype = src.substring(src.lastIndexOf('.'), src.length);
		 // check if the src already has an _on and delete it, if that is the case 
		 if(/_on/.test(src)){
		  newsrc = src.replace('_on','');
		  $(o).removeClass('on');
		  }else{
		  // else, add the _on to the src 
		  newsrc = src.replace(ftype, '_on'+ftype);
		  $(o).addClass('on');
		  }
		 o.src=newsrc;
	}

}



function goTo(url){
	parent.window.location.href=url;	
}

function bookmark(){
	var height = $('#container-outer').height();
	FB.ui({ method: 'bookmark.add' });
	$('#position_hack').css({'top': (height-100)/2});
	window.location.hash = 'position_hack';
}

function postToStream(title,caption,description,url,image){


FB.ui(
   {
     method: 'stream.publish',
     message: '',
     attachment: {
       name: title,
       caption: caption,
       description: (
         description
       ),
       href: url,
	   media: [
              {type: 'image',src: image, href: image} ]
     }
	  
	 //,
     //action_links: [
     //  { text: 'Code', href: 'http://github.com/facebook/connect-js' }
     //],
     //user_prompt_message: 'Share your thoughts about Connect'
   },
   function(response) {
     if (response && response.post_id) {
       //alert('Post was published.');
	   _gaq.push(['_trackEvent', 'Share', 'Content Shared', url]);
     } else {
     //  alert('Post was not published.');
     }
   }
 );

	//$('.fb_dialog_advanced').eq(1).css({ 'top':'100px !important', 'margin':'auto; !important'});
	//scrollTo(0,0);

	var top = (height-100)/2;
	scrollTo(0,top);

}

function invite(){
	FB.ui({
		  method:'fbml.dialog', 
		  fbml:'<fb:request-form action="URL" method="post" invite="true" type="TYPE" content="THIS IS THE MESSAGE <fb:req-choice url=\'URL\'label=\'Accept\' />" <fb:multi-friend-selector showborder="false" actiontext="REQUEST FORM TITLE" /> </fb:request-form>'
		  });
}




function singleRequest(user_id){
	
	action = $('#site_url').val() + '/invite/reminder';
	content = 'Please come to MASSCanvas and vote for my designs';
	
	//alert(action);
	
	inviteDialog = new FB.ui(
    {
        method: 'fbml.dialog',
       display: 'iframe',
        width: '200px',
		height: '100px',
        fbml: (
                //'<img style="padding:10px;" width="75" height="75" src="'+imglink+'" /><br /><br /><br />'+
				
				'<div style="text-align:center">' + 
                '<fb:fbml>'+
					'<div style="background:#3B5998"><fb:header icon="false" decoration="no_padding"><div style="text-align:left; color: #ffffff; background:#3b5998; padding: 10px 10px">Invitation Reminder</div></fb:header></div><br />' + 
					'Click the button below to send another invitation<br /><br />' + 
					'<fb:request-form '+
                        'action="'+ action + '" '+
                        'target="_self" '+
                        'method="POST" '+
                        'invite="true" '+
                        'type="Invite" '+
                        'content="'+content+
                            '<fb:req-choice '+
                                'url=\''+ $('#canvas_url').val() +'\' '+
                                'label=\'Go!\' '+
                                '/>"'+
                        '>'+
                        '<fb:request-form-submit uid="'+user_id+'" onclick="alert(\'test\');"/>'+
                    '</fb:request-form>'+
                '</fb:fbml></div>'
                )
    },
    function(response) {
       // inviteDialog.hide();
    });
	 console.debug(FB.UIServer);

	//$(".FB_UI_Dialog").css('width', $(window).width()*0.2); // 80% of window width
}

function requestSent(ids){
		//alert('sent to ' + ids);
		//inviteDialog.close();
		$(".FB_UI_Dialog").remove(); // 80% of window width
		
}


function switchTab(tab_id){
	
	if (!$('#tab' + tab_id).hasClass('selected')){
		$('.tab').removeClass('selected');
		$('.tab-content').hide();
		$('#tab' + tab_id).addClass('selected');
		$('#tab-content' + tab_id).fadeIn('slow');
	}
	Cufon.refresh();
}

function vote(entry_id){
	//alert('Vote?');

	
	$.ajax({
	  url: $('#site_url').val() + '/vote/' +  entry_id,
	  success: function(result) {	
		//$('#dialog').html(result);
		//dialogShow('dialog');
		
		var data = jQuery.parseJSON(result);
		//alert(data.msg);
		if (data.msg == "not_logged_in"){
			top.location.href= data.login_url;	
		}
		if (data.msg == "success"){
			//_gaq.push(['_trackEvent', 'Vote', 'Vote On Design',design_id]);
		}
		if (data.msg == "fail"){
			//_gaq.push(['_trackEvent', 'Remove Vote', 'Remove Vote On Design',design_id]);
		}
		
	  }
	});	
}


function cancelInvite(invite_id){
	//alert('cancel invite!' + invite_id);	
	$('#sent-' + invite_id).slideUp();
	$.ajax({
	  url: $('#site_url').val() + '/invite/cancel?id=' + invite_id,
	  success: function(result) {}
	});
}

function callback(){
	//Do nothing	
}




//Simple slideshow:

var i=0;
var jump=1;


function nextSlide(){
	
	
	var num_slides = $('#slides li').size();
	var step= $('#slides li:first').width();
	//alert("Next");
	i = i+1;	
	//alert(i);
	x = i*step*jump;
	if ((x/step) >= num_slides){
		x = 0;
		i = 0;
	}
	//alert(step + ' ' + num_slides + ' ' + x + ' ' + i);
	$('#slides').animate({'left': 0 - x + 'px'},300);
	
}

function prevSlide(){
/* <![CDATA[ */
	
	//alert(i);
	var num_slides = $('#slides li').size();
	var step= $('#slides li:first').width();
	
	if (i > 0){
		i = i-1;
		x = i*step*jump;
		if (x < 0){
			x = 0;
			i = 0;
		}
		//alert(x + ' ' + i);
		$('#slides').animate({'left': 0 - x + 'px'},300);
	}
	
}

function goToSlide(num){
	var num_slides = $('#slides li').size();
	var step= $('#slides li:first').width();
	x = num*step*jump;
	$('#slides').animate({'left': 0 - x + 'px'},300);
}

function toggleSubMenu(id){
	$('#' + id).next('.sub-menu').slideToggle();	
}

function subscribe(){
	$.post($('#site_url').val() + '/newsletter',$('#subscribe-form').serialize(), function(data){
			//alert(data.success);
			if (data.success == "true"){
				$('#newsletter-form .dialog-content').html('<div class="copperplate" style="font-size: 16px">Thanks for signing up!</div>');
				Cufon.refresh();
			}
			else {
				$('#newsletter-form .dialog-content').html('<div class="copperplate" style="font-size: 16px">Sorry, there was an error during sign-up.</div>');
				Cufon.refresh();
			}
		}, "json"
  );	
}
