(function($) {
  $.reskutusu = function(data) {
    reskutusu_init()
    reskutusu_loading()
    $.isFunction(data) ? data.call(this, $) : reskutusu_reveal(data)
    return $
  }
  var url = window.location;
  $.reskutusu.settings = {
    loading_image : 'http://www.ctsotomasyon.com/grafikler/loading.gif',
    close_image   : 'http://www.ctsotomasyon.com/grafikler/kapathover.gif',
    image_types   : [ 'png', 'jpg', 'jpeg', 'gif' ],
    next_image    : '/grafikler/nexthover.gif',
    prev_image    : '/grafikler/prevhover.gif',
    reskutusu_html  : '\
  <div id="res-container"><div id="res-overlay"></div><div id="reskutusu" style="display:none;"> \
    <div class="popup"> \
      <table> \
        <tbody> \
          <tr> \
            <td class="body"> \
              <div class="content"> \
              </div> \
              <div class="info"></div> \
              <div class="footer"> \
                <div class="navigation"></div> \
                <a href="#" class="close"> \
                  <img src="http://www.ctsotomasyon.com/grafikler/kapathover.gif" title="Kapat" class="close_image" /> \
                </a> \
              </div> \
            </td> \
          </tr> \
        </tbody> \
      </table> \
    </div> \
  </div></div>'
  }
  var $s = $.reskutusu.settings
  
  $.fn.reskutusu = function(settings) {
    reskutusu_init(settings)

    var image_types = $s.image_types.join('|')
    image_types = new RegExp('\.' + image_types + '$', 'i')

    // suck out the images
    var images = []
    $(this).each(function() {
      if (this.href.match(image_types) && $.inArray(this.href, images) == -1) 
        images.push(this.href)
    })
    if (images.length == 1) images = null 

    function click_handler() {
      if ($('#reskutusu .loading').length == 1) return false
      reskutusu_loading()

      // support for rel="reskutusu[.inline_popup]" syntax, to add a class
      var klass = this.rel.match(/reskutusu\[\.(\w+)\]/)
      if (klass) klass = klass[1]

      // div
      if (this.href.match(/#/)) {
        var url    = window.location.href.split('#')[0]
        var target = this.href.replace(url,'')
        reskutusu_reveal($(target).clone().fadeIn(200), klass)

      // image
      } else if (this.href.match(image_types)) {
	   var title = this.title;
        reskutusu_reveal_image(this.href, title, images, klass)

      // ajax
      } else {
        $.get(this.href, function(data) { reskutusu_reveal(data, klass) })
      }

      return false
    }

    return this.click(click_handler)
  }

/**
  * The init function is a one-time setup which preloads vital images
  * and other niceities.
  */
  function reskutusu_init(settings) {
    if ($s.inited && typeof settings == 'undefined')
      return true
    else 
      $s.inited = true

    if (settings) $.extend($s, settings)

    $('body').append($s.reskutusu_html)

    var preload = [ new Image(), new Image() ]
    preload[0].src = $s.close_image
    preload[1].src = $s.loading_image

    $('#reskutusu').find('.b:first, .bl, .br, .tl, .tr').each(function() {
      preload.push(new Image())
      preload.slice(-1).src = $(this).css('background-image').replace(/url\((.+)\)/, '$1')
    })
    $('#reskutusu .close').click(reskutusu_close),
    $('#reskutusu .close_image').attr('src', $s.close_image);
	  	$('#reskutusu .close_image').each(function(a){
		$(this).hover(function(){
		 $(this).attr("src","http://www.ctsotomasyon.com/grafikler/closelabel.gif");
		}, function(){
		 $(this).attr("src","http://www.ctsotomasyon.com/grafikler/kapathover.gif");
		});
	});
  }

/**
  * The loading function prepares the reskutusu by displaying it
  * in the proper spot, cleaning its contents, attaching keybindings 
  * and showing the loading image.
  */
  function reskutusu_loading() {
    if ($('#reskutusu .loading').length == 1) return true

    $(document).unbind('.reskutusu')
    $('#reskutusu .content, #reskutusu .info, #reskutusu .navigation').empty()
    $('#reskutusu .body').children().hide().end().
      append('<div class="loading"><img src="'+$s.loading_image+'"/></div>')

    var pageScroll = getPageScroll()
    $('#reskutusu').css({
      top:	pageScroll[1] + (getPageHeight() / 5),
      left:	((getPageWidth() / 2) - ($('#reskutusu').width() / 2))
    }).show(200)

    $(document).bind('keydown.reskutusu', function(e) {
      if (e.keyCode == 27) reskutusu_close()
    })
  }

/**
  * The reskutusu_reveal function sets the user-defined class (if any)
  * on the .content div, removes the loading image, and displays
  * the data.  If an extra_setup functino is provided, it will be run
  * right before the data is displayed but after it is added.
  */
  function reskutusu_reveal(data, klass, extra_setup) {
    $('#reskutusu .content').addClass(klass).append(data)
    $('#reskutusu .loading').remove()
    if ($.isFunction(extra_setup)) extra_setup.call(this)
    $('#res-overlay').fadeIn('normal')
    $('#reskutusu .body > *').fadeIn('normal')
  }

/**
  * Used to load and show an image in the reskutusu.  Involved in the slideshow business.
  */
  function reskutusu_reveal_image(href, title, images, klass) {
    if (images) var extra_setup = reskutusu_setup_gallery(href, title, images, klass)
    var image    = new Image()
    image.onload = function() {
      reskutusu_reveal('<div class="image"><img src="' + image.src + '" title="' + title + '" /></div>', klass, extra_setup)
      // load the next image in the background
      if (images) {
        var position = $.inArray(href, images)
        var next = new Image()
        next.src = images[position+1] ? images[position+1] : images[0]
      }
    }
    image.src = href
  }

/**
  * Unbinds all listeners and closes the reskutusu.
  */
  function reskutusu_close() {
    $('#res-overlay').fadeOut();
    $(document).unbind('.reskutusu');
    $('#reskutusu').fadeOut(function() {
      $('#reskutusu .content').removeClass().addClass('content')
    })
    return false
  }

  function reskutusu_setup_gallery(href, title, images, klass) {
    var position = $.inArray(href, images)

    var jump = function(where) {
      reskutusu_loading()
      if (where >= images.length) where = 0
      if (where < 0) where = images.length - 1
      reskutusu_reveal_image(images[where], href, images, klass)
    }

    return function() {
      $('#reskutusu .image').click(function() { jump(position + 1) }).css('cursor', 'pointer')
      $('#reskutusu .info').append('Resim ' + (position + 1) + ' / ' + images.length +
								   '<span class="resBaslik">' + title + '</span>')
      $('#reskutusu .navigation').
        append('<img class="prev" src="' + $s.prev_image + '" title="Geri"/>' +
			   '<img class="next" src="' + $s.next_image + '" title="İleri"/>').
        find('img').css('cursor', 'pointer').end().
        find('.prev').click(function() { jump(position - 1); return false }).end().
        find('.next').click(function() { jump(position + 1); return false }).end()
	
	$('#reskutusu .next').each(function(a){
		$(this).hover(function(){
		 $(this).attr("src","http://www.ctsotomasyon.com/grafikler/next.gif");
		}, function(){
		 $(this).attr("src","http://www.ctsotomasyon.com/grafikler/nexthover.gif");
		});
	});
	$('#reskutusu .prev').each(function(a){
		$(this).hover(function(){
		 $(this).attr("src","http://www.ctsotomasyon.com/grafikler/prev.gif");
		}, function(){
		 $(this).attr("src","http://www.ctsotomasyon.com/grafikler/prevhover.gif");
		});
	});

      $(document).bind('keydown.reskutusu', function(e) {
        if (e.keyCode == 39) jump(position + 1) // right
        if (e.keyCode == 37) jump(position - 1) // left
      })
    }
  }

  function reskutusu_start_slideshow() {
    $('#reskutusu .play').attr('src', $s.pause_image)
    $s.playing = setInterval(function() { $('#reskutusu .next').click() }, $s.slide_duration * 1000)
  }

  function reskutusu_stop_slideshow() {
    $('#reskutusu .play').attr('src', $s.play_image)
    clearInterval($s.playing)
    $s.playing = false
  }

  // getPageScroll() by quirksmode.com
  function getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
      yScroll = self.pageYOffset; xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
      yScroll = document.documentElement.scrollTop; xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop; xScroll = document.body.scrollLeft;	
    }
    return new Array(xScroll,yScroll) 
  }

  // adapter from getPageSize() by quirksmode.com
  function getPageHeight() {
    var windowHeight
    if (self.innerHeight) {	// all except Explorer
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowHeight = document.body.clientHeight;
    }	
    return windowHeight
  }
  function getPageWidth() {
	  var windowWidth;
	  if( typeof( window.innerWidth ) == 'number' ) {
	    windowWidth = window.innerWidth; //Non-IE
	  } else if( document.documentElement && ( document.documentElement.clientWidth ) ) {
	    windowWidth = document.documentElement.clientWidth; //IE 6+ in 'standards compliant mode'
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    windowWidth = document.body.clientWidth; //IE 4 compatible
	  }
	  return windowWidth
	} 
})(jQuery);
(function($) {
  $.frame = function(data, klass) {
    $.frame.loading()

    if (data.ajax) fillframeFromAjax(data.ajax)
    else if (data.image) fillframeFromImage(data.image)
    else if (data.div) fillframeFromHref(data.div)
    else if ($.isFunction(data)) data.call($)
    else $.frame.reveal(data, klass)
  }

  $.extend($.frame, {
    settings: {
      opacity      : 40,
      overlay      : true,
		next_image    : '/grafikler/nexthover.gif',
		prev_image    : '/grafikler/prevhover.gif',
      imageTypes   : [ 'png', 'jpg', 'jpeg', 'gif' ],
      frameHtml  : '\
      <div id="res-container"><div id="res-overlay"></div><div id="reskutusu" style="display:none;"> \
      <div class="popup"> \
        <table> \
          <tbody> \
            <tr> \
              <td class="body"> \
                <div class="content"> \
                </div> \
                <div class="footer"> \
                  <a href="#" class="close"> \
                    <img src="http://www.ctsotomasyon.com/grafikler/kapathover.gif" title="Kapat" class="close_image" /> \
                  </a> \
                </div> \
              </td> \
            </tr> \
          </tbody> \
        </table> \
      </div> \
    </div>'
    },

    loading: function() {
      init()
      if ($('#frame .loading').length == 1) return true
      showOverlay()

      $('#frame .content').empty()
      $('#frame .body').children().hide().end().
        append('<div class="loading"><img src="'+$.frame.settings.loadingImage+'"/></div>')

      $('#frame').css({
        top:	getPageScroll()[1] + (getPageHeight() / 10),
        left:	385.5
      }).show()

      $(document).bind('keydown.frame', function(e) {
        if (e.keyCode == 27) $.frame.close()
        return true
      })
      $(document).trigger('loading.frame')
    },

    reveal: function(data, klass) {
      $(document).trigger('beforeReveal.frame')
      if (klass) $('#frame .content').addClass(klass)
      $('#frame .content').append(data)
      $('#frame .loading').remove()
      $('#frame .body').children().fadeIn('normal')
      $('#frame').css('left', $(window).width() / 2 - ($('#frame table').width() / 2))
      $(document).trigger('reveal.frame').trigger('afterReveal.frame')
    },

    close: function() {
      $(document).trigger('close.frame')
      return false
    }
  })

  /*
   * Public, $.fn methods
   */

  $.fn.frame = function(settings) {
    init(settings)

    function clickHandler() {
      $.frame.loading(true)

      // support for rel="frame.inline_popup" syntax, to add a class
      // also supports deprecated "frame[.inline_popup]" syntax
      var klass = this.rel.match(/frame\[?\.(\w+)\]?/)
      if (klass) klass = klass[1]

      fillframeFromHref(this.href, klass)
      return false
    }

    return this.click(clickHandler)
  }

  /*
   * Private methods
   */

  // called one time to setup frame on this page
  function init(settings) {
    if ($.frame.settings.inited) return true
    else $.frame.settings.inited = true

    $(document).trigger('init.frame')
    makeCompatible()

    var imageTypes = $.frame.settings.imageTypes.join('|')
    $.frame.settings.imageTypesRegexp = new RegExp('\.' + imageTypes + '$', 'i')

    if (settings) $.extend($.frame.settings, settings)
    $('body').append($.frame.settings.frameHtml)

    var preload = [ new Image(), new Image() ]
    preload[0].src = $.frame.settings.closeImage
    preload[1].src = $.frame.settings.loadingImage

    $('#frame').find('.b:first, .bl, .br, .tl, .tr').each(function() {
      preload.push(new Image())
      preload.slice(-1).src = $(this).css('background-image').replace(/url\((.+)\)/, '$1')
    })

    $('#frame .close').click($.frame.close)
    $('#frame .close_image').attr('src', $.frame.settings.closeImage)
  }
  
  // getPageScroll() by quirksmode.com
  function getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
      yScroll = self.pageYOffset;
      xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
      yScroll = document.documentElement.scrollTop;
      xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop;
      xScroll = document.body.scrollLeft;	
    }
    return new Array(xScroll,yScroll) 
  }

  // Adapted from getPageSize() by quirksmode.com
  function getPageHeight() {
    var windowHeight
    if (self.innerHeight) {	// all except Explorer
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowHeight = document.body.clientHeight;
    }	
    return windowHeight
  }

  // Backwards compatibility
  function makeCompatible() {
    var $s = $.frame.settings

    $s.loadingImage = $s.loading_image || $s.loadingImage
    $s.closeImage = $s.close_image || $s.closeImage
    $s.imageTypes = $s.image_types || $s.imageTypes
    $s.frameHtml = $s.frame_html || $s.frameHtml
  }

  // Figures out what you want to display and displays it
  // formats are:
  //     div: #id
  //   image: blah.extension
  //    ajax: anything else
  function fillframeFromHref(href, klass) {
    // div
    if (href.match(/#/)) {
      var url    = window.location.href.split('#')[0]
      var target = href.replace(url,'')
      $.frame.reveal($(target).clone().show(), klass)

    // image
    } else if (href.match($.frame.settings.imageTypesRegexp)) {
      fillframeFromImage(href, klass)
    // ajax
    } else {
      fillframeFromAjax(href, klass)
    }
  }

  function fillframeFromImage(href, klass) {
    var image = new Image()
    image.onload = function() {
      $.frame.reveal('<div class="image"><img src="' + image.src + '" /></div>', klass)
    }
    image.src = href
  }

  function fillframeFromAjax(href, klass) {
    $.get(href, function(data) { $.frame.reveal(data, klass) })
  }

  function skipOverlay() {
    return $.frame.settings.overlay == false || $.frame.settings.opacity === null 
  }

  function showOverlay() {
    if (skipOverlay()) return

    if ($('frame_overlay').length == 0) 
      $("body").append('<div id="frame_overlay" class="frame_hide"></div>')

    $('#frame_overlay').hide().addClass("frame_overlayBG")
      .css('opacity', $.frame.settings.opacity)
      .click(function() { $(document).trigger('close.frame') })
      .fadeIn(200)
    return false
  }

  function hideOverlay() {
    if (skipOverlay()) return

    $('#frame_overlay').fadeOut(200, function(){
      $("#frame_overlay").removeClass("frame_overlayBG")
      $("#frame_overlay").addClass("frame_hide") 
      $("#frame_overlay").remove()
    })
    
    return false
  }

  /*
   * Bindings
   */

  $(document).bind('close.frame', function() {
    $(document).unbind('keydown.frame')
    $('#frame').fadeOut(function() {
      $('#frame .content').removeClass().addClass('content')
      hideOverlay()
      $('#frame .loading').remove()
    })
  })

})(jQuery);
 
