// jQuery mod functions
jQuery.fn.center = function() {
    this.css("position", "absolute");
    //this.css("top", ($(window).height() - this.height()) / 2 - $(window).scrollTop() + "px");
    this.css("top", $(window).scrollTop() + 50 + "px");
    this.css("left", ($(window).width() - this.width()) / 2 - $(window).scrollLeft() + "px");
    return this;
}

function getImgSize(imgSrc) {
    var newImg = new Image();
    newImg.src = imgSrc;
    var height = newImg.height;
    var width = newImg.width;
    return width;
}

//Modal Window functions
function launchWindow(id) {
    
    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set heigth and width to mask to fill up the whole screen
    $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

    //transition effect
    $('#mask').fadeTo("fast", 0.75);
    $('#mask').fadeIn(1000);

    //Get the window height and width
    //var winH = $(window).height();
    //var winW = $(window).width();

    //Set the popup window to center
    //$(id).css('top', winH / 2 - $(id).height());
    //$(id).css('left', winW / 2 - $(id).width() / 2);
    $(id).center();
    
    //transition effect
    $(id).fadeIn(2000);
}

function launchSWFWindow(id, src){

    var playerVersion = swfobject.getFlashPlayerVersion(); // returns a JavaScript object
    var majorVersion = playerVersion.major; // access the major, minor and release version numbers via their respective properties
    
    if (majorVersion >= 8) {
       $(id + ' #modalSWF').html('<embed src="'+ src +'" width="425" height="388" allowscriptaccess="always" wmode="opaque" quality="high" bgcolor="#000000" name="'+ id +'-swffile" id="'+ id +'-swffile" style="" type="application/x-shockwave-flash">');
    } else {
       $(id + ' #modalSWF').html('Flash player is not installed');
    }
    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set heigth and width to mask to fill up the whole screen
    $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

    //transition effect
    $('#mask').fadeTo("fast", 0.75);
    $('#mask').fadeIn(1000);

    //Get the window height and width
    //var winH = $(window).height();
    //var winW = $(window).width();

    //Set the popup window to center
    //$(id).css('top', winH / 2 - $(id).height());
    //$(id).css('left', winW / 2 - $(id).width() / 2);
    $(id).center();
    
    //transition effect
    $(id).fadeIn(2000);
}

function launchIMGWindow(id, src, link){
   
    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();
    
	//Set heigth and width to mask to fill up the whole screen
    $('#mask').css({ 'width': maskWidth, 'height': maskHeight });
	$('#mask').css("background-image", "url('/templates/ost-magazine/images/ajax-loader.gif')");
	$('#mask').css("background-position", "center center");
	$('#mask').css("background-repeat", "no-repeat");
    
	//transition effect
    $('#mask').fadeTo("fast", 0.75);
    $('#mask').fadeIn(1000);
   
   
    $(id + ' #modalIMG').html('<a href="'+ link +'"><img class="modal-image" src="" /></a>');
    
    $(id + ' #modalIMG img.modal-image').attr('src', src).load(function() {  
        
        $('#mask').css("background-image", "none");
		
		imgWidth = getImgSize(src);
        
        $(id + ' #modalContent').css("width", imgWidth);    
        $(id + ' #modalIMG').css("width", imgWidth);
    
        //Get the window height and width
        //var winH = $(window).height();
        //var winW = $(window).width();
    
        //Set the popup window to center
        //$(id).css('top', winH / 2 - $(id).height());
        //$(id).css('left', winW / 2 - $(id).width() / 2);
        $(id).center();
        
        //transition effect
        $(id).fadeIn(2000);
    
    }).each(function(){
    if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6))
       $(this).trigger("load");
    });
}

$(document).ready(function() {
      
      // Admin functions
	  $('form #twitter').change(function() {
	    $('#twitter_form').toggle('normal');
	    $('form #twitter_count').html('<span>' + (138 - $('form #twitter_content').val().length) + ' characters remaining</span>');
	  });
		
	  $('form #twitter_content').keyup(function() {
	    var new_length = 138 - $(this).val().length;
	    if(new_length <= 0) {
	        $('form #twitter_count').addClass('formErrors');
	    } else {
	        $('form #twitter_count').removeClass('formErrors');
	    }
	    $('form #twitter_count').html('<span>' + new_length + ' characters remaining</span>');
	  }); 
	  
	  //select all the a tag with name equal to modal
      $('a[name=modal]').click(function(e) {
          //Cancel the link behavior
          e.preventDefault();
          //Get the A tag
          var id = $(this).attr('href');
          launchWindow(id);
      });
      
      $('a[name=swfmodal]').click(function(e) {
          //Cancel the link behavior
          e.preventDefault();
          //Get the A tag
          
          var idsrc = $(this).attr('href');
          var strArray = idsrc.split("|");
          var id = strArray[0];
          var src = strArray[1];
          launchSWFWindow(id,src);
          
      });
      
      $('a[name=imgmodal]').click(function(e) {
          //Cancel the link behavior
          e.preventDefault();
          //Get the A tag
          
          var idsrc = $(this).attr('href');
          var strArray = idsrc.split("|");
          var id = strArray[0];
          var src = strArray[1];
          var link = strArray[2];
          launchIMGWindow(id,src,link);
          
      });
     
      // MODAL WINDOW CONTROLS
      //if close button is clicked  
      $('.window .close').click(function(e) {
          if ($(this).hasClass('go') == false) {
              //Cancel the link behavior  
              e.preventDefault();
          }
          $('#mask, .window').hide();
          $("#modalPlayerWin #modalSWF").html('');
          $("#modalViewerWin #modalSWF").html('');
      });
      
      //if mask is clicked  
      $('#mask').click(function() {
          $(this).hide();
          $('.window').hide();
          $("#modalPlayerWin #modalSWF").html('');
          $("#modalViewerWin #modalSWF").html('');
      });
	  
	  // Setup some FB thumbs
	  $('img:not(form img)').each(function(){
		var src = $(this).attr('src');
		if(src.search(/ttp:\/\//i) == -1) {
			$('#fbthumbs').append('<link rel="image_src" href="http://'+ document.domain + $(this).attr('src') +'" />');
		}
	  });
	  
	  $("#blogentry_photo .photoblog-photo img").click(function() {
		// perform exposing for the clicked element
        $(this).expose({
			'color': "#000"
		});
      });
	  
	  $("#blogentry_photo .photoblog-photo img").mouseover(function() {
        var pinLink = '<a target="_blank" href="http://pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.apertureaffect.com&media=http://apertureaffect.com'+ $(this).attr('src') +'&description=Photography%20by%20Justin%20Bane" class="pin-it-button" count-layout="none"><img src="http://passets-cdn.pinterest.com/images/pinit_preview_none.png" /></a>';
		// show pinterest button for img
        if($(this).parent().find('.pin-it-button') != pinLink) {
			$(this).parent().append(pinLink);
		}
      });	  
     
  });

