jQuery(document).ready(function($){

var isDragging=false; ///drag event for the Helper							
var overLightbox=false; ///if is over the lightbox							
var theiconAdd=""; ///Drag to lightbox icon							
var theiconAddInside=""; ///Drag to lightbox icon							
var islightboxtriggered= false;
var BaseUrl2= "http://www.signaturecreative.com/2011";
// For use within normal web clients 
var isiPad = navigator.userAgent.match(/iPad/i) != null;

/*
var iPadwrapper = $(wrapper), iPadscrollable = $(scrollable);
var iPadinactiveMargin = 100;         
var iPadwrapperWidth = iPadwrapper.width();
var iPadwrapperHeight = iPadwrapper.height();
var iPadscrollableHeight = iPadscrollable.outerHeight() + 2*iPadinactiveMargin;

var iPadtop = (e.pageY -  iPadwrapperOffset.top) * (iPadscrollableHeight - iPadwrapperHeight) / iPadwrapperHeight  - iPadinactiveMargin;
    if (top < 0){
      top = 0;
    }
   iPadwrapper.scrollTop(top);
*/




////////////////////////////////////////////////////////////////////////////////// Scroll
$(document).mousemove(function(e){
  var themousepositionX = e.pageX;
  if ( themousepositionX > 300 && !isDragging && $('#drophere').is(":visible") ==true){
	closeLightbox();
	overLightbox=false;
  } else if ( themousepositionX > 300 && isDragging && $('#drophere').is(":visible") ==true){
		overLightbox=false;
  } else if ( themousepositionX < 300 && isDragging && $('#drophere').is(":visible") ==true){
		overLightbox=true;
  } else if ( themousepositionX < 300 && !isDragging && $('#drophere').is(":visible") ==true){
		overLightbox=true;
  }
}); 						

		
	
/**************************************************************************
* Open and Close Lightbox
*
**********
*
* open and close the lightbox
*/

function openLightbox() {
	
	if (isiPad){
	$(".toolwhite").fadeOut();  
	$('body').css({"overflow": "hidden"});
	if($("#overlay").length <= 0){
	$("<div>").attr("id", "overlay").css("opacity", 0).appendTo("body").animate({opacity:0.7 }, 200,'easeOutCubic');
	}
	$("#work_handle").slideUp();	  
	$("#drophere").show();	  
	$("#lightboxsc").animate({left: 0}, 300,'easeOutQuad');
	$("#lightbox_handle").animate({left: -50}, 300,'easeOutQuad');
	} else {
	$('body').css({"overflow": "hidden"});
	if($("#overlay").length <= 0){
	$("<div>").attr("id", "overlay").css("opacity", 0).appendTo("body").animate({opacity:0.7 }, 200,'easeOutCubic');
	}
	$("#work_handle").slideUp();	  
	$("#drophere").show();	  
	$("#lightboxsc").animate({left: 0}, 300,'easeOutQuad');
	$("#lightbox_handle").animate({left: -50}, 300,'easeOutQuad');
	$("#header").animate({left: 248}, 300,'easeOutQuad');
	$("#right_column").animate({left: 248}, 300,'easeOutQuad');
		
	}
}

function closeLightbox() {
	
	if (isiPad){
	$("#overlay").remove();
	$("#work_handle").slideDown();	  
	$("#drophere").hide();	  

	$("#lightboxsc").animate({left: -300}, 300,'easeOutCubic');
	$("#lightbox_handle").animate({left: 0}, 600,'easeOutQuad');
	$("#overlay").remove();
	$('body').css({"overflow": "visible"});
	} else {
	$("#overlay").remove();
	$("#work_handle").slideDown();	  
	$("#drophere").hide();	  

	$("#lightboxsc").animate({left: -300}, 300,'easeOutCubic');
	$("#lightbox_handle").animate({left: 0}, 600,'easeOutQuad');
	$("#header").animate({left: 0}, 300,'easeOutQuad');
	$("#right_column").animate({left: 0}, 300,'easeOutQuad');
	$("#overlay").remove();
	$('body').css({"overflow": "visible"});
	}
}


$("#overlay").live('click', function(){
	closeLightbox();								 
});
////// Close/Open Lightbox Button

$("#lightbox_handle").bind('mouseover',function() {
	openLightbox();
	overLightbox==true;
});

$("#close_lightbox").click(function(){
	closeLightbox();
});
$(".open_lightbox").click(function(){
	openLightbox();
								   
});


///// LAUNCH WORK FROM LIGHTBOX

	$(".lightbox_row a.view_work").live('click',function(){
		var href = $(this).attr( "href" );
		$.bbq.pushState({ url: href });
		closeLightbox();
		return false; 
	}); 

	
/**************************************************************************
* Drag
*
**********
*
* Drag to the lightbox
*/

  
var slices = $(".works");  
var workthumbs = $(".work_thumb");  
var thumbimg = $("img.dragtopdf");  



$(".dragtopdf").bind("mouseover", function() { 
	theiconAdd=$(this);
	theTitle=theiconAdd.parent().find('h2 a').html();
	theID=theiconAdd.parent().attr('title');
	theWorkSlug=theiconAdd.parent().children('.work_slug').val();
	thePDFurl=theiconAdd.parent().children('.work_pdf').val();
	theImgSrc = theiconAdd.parent().children(".imageurl").text();
	//make images draggable
});
	 $(".dragtopdf").draggable({
	  //create draggable helper
	  helper: function() {
		return $("<div class='wp-post-image'>").attr("id", "helper").html("<span> Add<br/><span class='drag_title'>" + theTitle + "</span><br/> to your Lightbox</span><input type='hidden' class='postid' name='postid' value='"+theID+"'/><input type='hidden' class='workslug' name='workslug' value='"+theWorkSlug+"'/><input type='hidden' class='pdfurl' name='pdfurl' value='"+thePDFurl+"'/><div class='helper_img'><img title='"+ theTitle +"' class='drag_thumb' src='" + theImgSrc + "'></div>").appendTo("body");
	  },
	  cursor: "pointer",
	  cursorAt: { left: 10, top: 20 },
	  zIndex: 99999,
	  //show overlay and targets
	  start: function() {
		isDragging=true;
		openLightbox();
		//$(this).unbind("mouseenter");
	  },
	  //remove targets and overlay
	  stop: function() {
		if(!overLightbox){
			closeLightbox();
			isDragging=false;
		} 
	  }
	  
	}).addTouch();
//make targets droppable
$("#drophere").droppable({
	accept: '.dragtopdf',
	tolerance: "pointer",
  //show info when over target
	over: function() {
		$('#lightboxsc').addClass('lightboxover');
		if ($('#first_drop').length){
			//$('#first_drop').fadeOut(300, function(){$('#first_drop').remove()});
			$('#first_drop').fadeOut(300);
		};
		//$("<span>").addClass("share").text("Share on " + $(this).attr("id")).addClass("active").appendTo($(this)).fadeIn();
  },
	out: function() {
		$('#lightboxsc').removeClass('lightboxover');
		//$("<span>").addClass("share").text("Share on " + $(this).attr("id")).addClass("active").appendTo($(this)).fadeIn();
  },
	drop: function() {
		
		
		$('#lightboxsc').removeClass('lightboxover');
		//theiconAdd.fadeOut();
		isDragging=false;  
		//itemID=  $("#helper").find('img').attr("title");
		var itemTitle=  $("#helper").find('img').attr("title");
		var itemImgSrc =  $("#helper").find('img').attr("src");
		var itemID =  $("#helper").find('input.postid').val();
		var pdfUrl =  $("#helper").find('input.pdfurl').val();
		var workslug =  $("#helper").find('input.workslug').val();
		if ($('#lightbox_'+ itemID).length < 1){
		//if(itemTitle){
		addToLighbox(itemTitle,itemImgSrc,itemID,pdfUrl,workslug) ;
	}
	//}
  }
});
	
	//////Add FROM :LIGHTBOX

function addToLighbox(itemTitle,itemImgSrc,itemID,pdfUrl,workslug) {
	NumberofWorks= parseInt($("#fieldid").val());
		/* $("#lightboxsc_list").append('<li id="lightbox_' + itemID + '" class="lightbox_row"><div class="lightboxsc_image"><a class="lightbox_remove tooltipwhite1" title="Delete" rel="' + itemID + '">delete</a><img src="' + itemImgSrc + '"></div><div class="lightboxsc_infos"><h4>'+itemTitle +'</h4><small>Category</small><div class="sort_navigation"><a href="#" class="insertbefore lightbox_sort">Before</a><a href="#" class="insertafter lightbox_sort">After</a></div></div></li>') ;*/
	 $("#lightboxsc_list").append('<li id="lightbox_' + itemID + '" class="lightbox_row"><div class="lightboxsc_image"><img src="' + itemImgSrc + '"></div><div class="lightboxsc_infos"><h4>'+itemTitle +'</h4><a href="work/'+workslug+'" class="view_work">&raquo; View Work</a><a class="lightbox_remove tooltipwhite1" title="Delete" rel="' + itemID + '">delete</a></div><div class="line_dark"></div></li>') ;
	$("#lightbox_" + itemID).hide().slideDown(500);
	$("#form_sendtoafriend #sentoafriend_submit").before('<input type="hidden" class="postid" name="postid[]" id="postid" value="'+itemID+'"/>');
	$("#form_pdf #pdf_cover").after('<input type="hidden" class="pdfurl" name="pdfurl[]" value="'+itemID+'"/>');
	$("#fieldid").val(NumberofWorks + 1);
	 $('#dragtopdf_'+itemID).addClass('inlightbox');
	//addSuccess(itemTitle);
	
}
function addToLighboxfromLink(itemTitle,itemImgSrc,itemID) {
	NumberofWorks= parseInt($("#fieldid").val());
	 $("#lightboxsc_list").append('<li id="lightbox_' + itemID + '" class="lightbox_row"><div class="lightboxsc_image"><img src="' + itemImgSrc + '"></div><div class="lightboxsc_infos"><h4>'+itemTitle +'</h4><small>Category</small><a class="lightbox_remove tooltipwhite1" title="Delete" rel="' + itemID + '">delete</a></div><div class="line_dark"></div></li>') ;
	 $('#dragtopdf_'+itemID).addClass('inlightbox');
	 //addSuccess(itemTitle);
	$("#lightbox_" + itemID).hide().delay(500).slideDown(300).delay(1000).show(500, function(){
		isDragging=false;
		islightboxtriggered=false;
		closeLightbox();
		});
	$("#form_sendtoafriend #sentoafriend_submit").before('<input type="hidden" class="postid" name="postid[]" id="postid" value="'+itemID+'"/>');
	$("#fieldid").val(NumberofWorks + 1);

	
}

function addSuccess(itemTitle){
		$("<div>").html('<span>'+itemTitle+' <br>Successfully Added!</span></div>').attr("id", "yay").appendTo("body").fadeIn(500).delay(500).fadeOut(300);
}

function addError(itemTitle){
		//$("<div>").html('<span>'+itemTitle+'Successfully Added!</span></div>').attr("id", "yay").appendTo("body").fadeIn(200,function(){$(this).fadeOut();});
		$("<div>").html('<span>'+itemTitle+' <br>Successfully Added!</span></div>').attr("id", "yay").appendTo("body").fadeIn(500).delay(500).fadeOut(300);
}

//////SORTABLE

		$("#lightboxsc_list").sortable({
			placeholder: 'li_highlight',
			axis: 'y',
			cursor: 'move',
			items: 'li',
			opacity: 0.7
			
		}).addTouch();


//////REMOVE FROM :LIGHTBOX
	
	$("a.lightbox_remove").live("click", function() {
	NumberofWorks= parseInt($("#fieldid").val());
	$("#fieldid").val(NumberofWorks - 1);									  
	//$(this).parent().parent().fadeOut(300);
	$(this).parent().parent().slideUp(300,function(){
		$(this).remove();
		if ($('.lightbox_row').length <= 0){
			//$('#first_drop').fadeOut(300, function(){$('#first_drop').remove()});
			$('#first_drop').fadeIn(300);
		};
	});
	ID=$(this).parent().parent().attr("id").substring(9);
	$('#dragtopdf_'+ID).removeClass('inlightbox');

	
	return false;
	
});

//////SEND TO A FRIEND


$("#lightbox_byemail").click(function(){
	$("#sendtoafriend").slideDown(200);	
	return false
});
<!------------ CALLBACK -------------->
$("#cancelform").click(function(){
	$('#form_sendtoafriend')[0].reset();
	$("#sendtoafriend").slideUp(200);	
	return false
});


$("#form_sendtoafriend").submit(function(){

var str = $(this).serialize();

$.ajax({
type: "POST",
url: BaseUrl2+"/wp-content/plugins/sc_lightbox/inc/process_sendtoafriend.php",
//url: "http://www.test.signaturecreative.com/wp-content/themes/signature2/process_sendtoafriend.php",
data: str,
success: function(msg){

$("#note").ajaxComplete(function(event, request, settings){

if(msg == 'OK') // Message Sent? Show the 'Thank You' message and hide the form
{

result = '<div class="notification_ok">Thank you!</div>';
$('#form_sendtoafriend')[0].reset();
}
else
{
result = msg;
}
$(this).html(result).slideDown(500).delay(1000).slideUp(500);
});
}
});

return false;

});

//// PDF Form


/**************************************************************************
*/

$('#download_lightbox').click(function() {
  $('#form_pdf').submit();
});

});
