// Open Window
function MM_reloadPage(init) {
  if (init == true) with (navigator) {
    if ((appName == "Netscape") && (parseInt(appVersion) == 4)) {
      document.MM_pgW = innerWidth; document.MM_pgH = innerHeight; onresize = MM_reloadPage;
    }
  }
  else if (innerWidth != document.MM_pgW || innerHeight != document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function OpenPictureWindow(imageUrl, imageWidth, imageHeight, alt, posLeft, posTop, isScrollVisible) {
  var scrollbars = (!isScrollVisible) ? "yes" : "no";

  newWindow = window.open("", "pictureWindow", "width=" + imageWidth + ",height=" + imageHeight + ",scrollbars=" + scrollbars + ",left=" + posLeft + ",top=" + posTop);
  newWindow.document.open();
  newWindow.document.write('<html><title>' + alt + '</title><body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0" onBlur="self.close()">');
  newWindow.document.write('<img src=\"' + imageUrl + '\" width=' + imageWidth + ' height=' + imageHeight + ' alt=\"' + alt + '\">');
  newWindow.document.write('</body></html>');
  newWindow.document.close();
  newWindow.focus();
}

function MM_openBrWindow(theURL, winName, features) {
  window.open(theURL, winName, features);
}

function OpenWindow(pageUrl, windowName, features) {
  newwindow = window.open(pageUrl, windowName, features);
  if (window.focus) { newwindow.focus() }
  return false;
}



////////////

/* 		the images preload plugin 		*/
(function ($) {
  $.fn.preload = function (options) {
    var opts = $.extend({}, $.fn.preload.defaults, options);
    o = $.meta ? $.extend({}, opts, this.data()) : opts;
    var c = this.length,
				l = 0;
    return this.each(function () {
      var $i = $(this);
      $('<img/>').load(function (i) {
        ++l;
        if (l == c) o.onComplete();
      }).attr('src', $i.attr('src'));
    });
  };
  $.fn.preload.defaults = {
    onComplete: function () { return false; }
  };
})(jQuery);

$(function () {

  var $tf_bg = $('#tf_bg'),
					$tf_bg_images = $tf_bg.find('img'),
					$tf_bg_img = $tf_bg_images.eq(0),
					$tf_thumbs = $('#tf_thumbs'),
					total = $tf_bg_images.length,
					current = 0,
					$tf_content_wrapper = $('#tf_content_wrapper'),
					$tf_next = $('#tf_next'),
					$tf_prev = $('#tf_prev'),
					$tf_loading = $('#tf_loading');

  //preload the images				
  $tf_bg_images.preload({
    onComplete: function () {
      $tf_loading.hide();
      init();
    }
  });


  function init() {
    var dim = getImageDim($tf_bg_img);
    //set the returned values and show the image
    $tf_bg_img.css({
      width: dim.width,
      height: dim.height,
      left: dim.left,
      top: dim.top
    }).fadeIn();

    //resizing the window resizes the $tf_bg_img
    $(window).bind('resize', function () {
      var dim = getImageDim($tf_bg_img);
      $tf_bg_img.css({
        width: dim.width,
        height: dim.height,
        left: dim.left,
        top: dim.top
      });
    });

    //expand and fit the image to the screen
    $('#tf_zoom').live('click',
		  function () {
		    if ($tf_bg_img.is(':animated'))
		      return false;

		    var $this = $(this);
		    if ($this.hasClass('tf_zoom')) {
		      resize($tf_bg_img);
		      $this.addClass('tf_fullscreen')
					   .removeClass('tf_zoom');
		    }
		    else {
		      var dim = getImageDim($tf_bg_img);
		      $tf_bg_img.animate({
		        width: dim.width,
		        height: dim.height,
		        top: dim.top,
		        left: dim.left
		      }, 350);
		      $this.addClass('tf_zoom')
					   .removeClass('tf_fullscreen');
		    }
		  }
	  );

    //click the arrow down, scrolls down
    $tf_next.bind('click', function () {
      if ($tf_bg_img.is(':animated'))
        return false;
      scroll('tb');
    });

    //click the arrow up, scrolls up
    $tf_prev.bind('click', function () {
      if ($tf_bg_img.is(':animated'))
        return false;
      scroll('bt');
    });

    //mousewheel events - down / up button trigger the scroll down / up
    $(document).mousewheel(function (e, delta) {
      if ($tf_bg_img.is(':animated'))
        return false;

      if (delta > 0)
        scroll('bt');
      else
        scroll('tb');
      return false;
    });

    //key events - down / up button trigger the scroll down / up
    $(document).keydown(function (e) {
      if ($tf_bg_img.is(':animated'))
        return false;

      switch (e.which) {
        case 38:
          scroll('bt');
          break;

        case 40:
          scroll('tb');
          break;
      }
    });
  }


  function scroll(dir) {
    //if dir is "tb" (top -> bottom) increment current,
    //else if "bt" decrement it

    current = (dir == 'tb') ? current + 2 : current - 2;
    var skipImage = (dir == 'tb') ? current - 1 : current + 1;

    if (current >= total) {
      current = current - total;
      skipImage = current - total + 1;
    }
    else if (current == -1) {
      skipImage = 0;
      current = total - 1;
    }
    else if (current < -1) {
      current = total - 2;
      skipImage = total - 1;
    }

    //flip the thumb
    $tf_thumbs.flip({
      direction: dir,
      speed: 400,
      onBefore: function () {
        //the new thumb is set here
        var content = '<span id="tf_zoom" class="tf_zoom"></span>';
        content += '<img src="' + $tf_bg_images.eq(current).attr('longdesc') + '" alt="Thumb' + (current + 1) + '"/>';
        $tf_thumbs.html(content);
      }
    });

    //we get the next image
    var $tf_bg_img_next = $tf_bg_images.eq(current),
				dim = getImageDim($tf_bg_img_next),
				top = (dir == 'tb') ? $(window).width() + 'px' : -parseFloat(dim.width, 10) + 'px';

    var $tf_bg_img_skip = $tf_bg_images.eq(skipImage), dimSkip = getImageDim($tf_bg_img_skip),
	      topSkip = (dir == 'tb') ? $(window).width() + 'px' : -parseFloat(dimSkip.width, 10) + 'px';

    //set the returned values and show the next image	
    $tf_bg_img_next.css({
      width: dim.width,
      height: dim.height,
      left: top,
      top: dim.top
    }).show();

    $tf_bg_img_skip.css({
      width: 3*dim.width,
      height: dim.height,
      left: top,
      top: dimSkip.top
    }).show();

    //now slide it to the viewport
    $tf_bg_img_next.stop().animate({
      left: dim.left
    }, 1000);

    $tf_bg_img_skip.stop().animate({
      left: dim.left
    }, 1);

    //we want the old image to slide in the same direction, out of the viewport
    var slideTo = (dir == 'tb') ? -$tf_bg_img.width() + 'px' : $(window).width() + 'px';
    var skipSlideTo = (dir == 'tb') ? -$tf_bg_img_skip.width() + 'px' : $(window).width() + 'px';
    $tf_bg_img.stop().animate({
      left: slideTo
    }, 1000, function () {
      //hide it
      $(this).hide();
      //the $tf_bg_img is now the shown image
      $tf_bg_img = $tf_bg_img_next;
      //show the description for the new image
      $tf_content_wrapper.children()
											   .eq(current)
										       .show();
    });
    $tf_bg_img_skip.stop().animate({
      left: skipSlideTo
    }, 1000, function () {
      //hide it
      $(this).hide();
    });

    //hide the current description	
    $tf_content_wrapper.children(':visible')
										   .hide()

  }

  //animate the image to fit in the viewport
  function resize($img) {
    var w_w = $(window).width(),
						w_h = $(window).height(),
						i_w = $img.width(),
						i_h = $img.height(),
						r_i = i_h / i_w,
						new_w, new_h;

    if (i_w > i_h) {
      new_w = w_w;
      new_h = w_w * r_i;

      if (new_h > w_h) {
        new_h = w_h;
        new_w = w_h / r_i;
      }
    }
    else {
      new_h = w_w * r_i;
      new_w = w_w;
    }

    $img.animate({
      width: new_w + 'px',
      height: new_h + 'px',
      top: '0px',
      left: '0px'
    }, 350);
  }

  //get dimentions of the image, 
  //in order to make it full size and centered
  function getImageDim($img) {
    var w_w = $(window).width(),
						w_h = $(window).height(),
						r_w = w_h / w_w,
						i_w = $img.width(),
						i_h = $img.height(),
						r_i = i_h / i_w,
						new_w, new_h,
						new_left, new_top;

    if (r_w > r_i) {
      new_h = w_h;
      new_w = w_h / r_i;
    }
    else {
      new_h = w_w * r_i;
      new_w = w_w;
    }


    return {
      width: new_w + 'px',
      height: new_h + 'px',
      left: (w_w - new_w) / 2 + 'px',
      top: (w_h - new_h) / 2 + 'px'
    };
  }
});


////////////  SLIDER END




/* SURVEY START */

$(function () {

  $("#surveyLink").click(function (e) {


    $("#surveyPage").load("/Assets/Handlers/SurveyHandler.ashx", function (response, status, xhr) {
      if (status == "error") {
        if (console)
          console.log("Hata oluştu : " + xhr.status + " " + xhr.statusText);
      }
      else {




        $("#sendAnswer").click(function (e) {
          var value = $("#surveyPage input:radio:checked").val();
          if (value && value != "") {


            $("#surveyPage").load("/Assets/Handlers/SurveyHandler.ashx?id=" + value, function (response, status, xhr) {
              if (status == "error") {
                if (console)
                  console.log("Hata oluştu : " + xhr.status + " " + xhr.statusText);
              }
            });



          }
          e.preventDefault();
        });
      }
    });
    e.preventDefault();
  });
});





/* SURVEY END */









/* COMPACT BOX */

$(function () {
  //caching
  //next and prev buttons
  var $cn_next = $('#cn_next');
  var $cn_prev = $('#cn_prev');
  //wrapper of the left items
  var $cn_list = $('#cn_list');
  var $pages = $cn_list.find('.cn_page');
  //how many pages
  var cnt_pages = $pages.length;
  //the default page is the first one
  var page = 1;
  //list of news (left items)
  var $items = $cn_list.find('.cn_item');
  //the current item being viewed (right side)
  var $cn_preview = $('#cn_preview');
  //index of the item being viewed. 
  //the default is the first one
  var current = 1;

  /*
  for each item we store its index relative to all the document.
  we bind a click event that slides up or down the current item
  and slides up or down the clicked one. 
  Moving up or down will depend if the clicked item is after or
  before the current one
  */
  $items.each(function (i) {
    var $item = $(this);
    $item.data('idx', i + 1);

    $item.bind('click', function () {
      var $this = $(this);
      $cn_list.find('.selected').removeClass('selected');
      $this.addClass('selected');
      var idx = $(this).data('idx');
      var $current = $cn_preview.find('.cn_content:nth-child(' + current + ')');
      var $next = $cn_preview.find('.cn_content:nth-child(' + idx + ')');

      if (idx > current) {
        $current.stop().animate({ 'top': '-300px' }, 600, 'easeOutBack', function () {
          $(this).css({ 'top': '310px' });
        });
        $next.css({ 'top': '310px' }).stop().animate({ 'top': '5px' }, 600, 'easeOutBack');
      }
      else if (idx < current) {
        $current.stop().animate({ 'top': '310px' }, 600, 'easeOutBack', function () {
          $(this).css({ 'top': '310px' });
        });
        $next.css({ 'top': '-300px' }).stop().animate({ 'top': '5px' }, 600, 'easeOutBack');
      }
      current = idx;
    });
  });

  /*
  shows next page if exists:
  the next page fades in
  also checks if the button should get disabled
  */
  $cn_next.bind('click', function (e) {
    var $this = $(this);
    $cn_prev.removeClass('disabled');
    ++page;
    if (page == cnt_pages)
      $this.addClass('disabled');
    if (page > cnt_pages) {
      page = cnt_pages;
      return;
    }
    $pages.hide();
    $cn_list.find('.cn_page:nth-child(' + page + ')').fadeIn();
    e.preventDefault();
  });
  /*
  shows previous page if exists:
  the previous page fades in
  also checks if the button should get disabled
  */
  $cn_prev.bind('click', function (e) {
    var $this = $(this);
    $cn_next.removeClass('disabled');
    --page;
    if (page == 1)
      $this.addClass('disabled');
    if (page < 1) {
      page = 1;
      return;
    }
    $pages.hide();
    $cn_list.find('.cn_page:nth-child(' + page + ')').fadeIn();
    e.preventDefault();
  });

});




/* COMPACT BOX END */




















////Tab Menu
//if (typeof jQuery != 'undefined') {
//  $(function() {
//    
//    // css
//    //  var w = $(window).width();
//    //  var h = $(window).height();

//    //  $('#site,#loading').css({
//    //    width: w,
//    //    height: h
//    //  });

//    // reset projects list
//    //plist(1);

//    // scrollTo click
//    $('#next').click(function() { block(2); });
//    //$('#button-right').click(function() { plist(2); });

//    //    // :hover ie fix :(
//    //    if ($.browser.msie) {
//    //      $('#menu').mouseover(function() { $(this).addClass('hover'); }).mouseout(function() { $(this).removeClass('hover'); });
//    //    }

//    //    // parteneri
//    //    var p = $('#parteneri').width();
//    //    var l = (parseInt(w) - parseInt(p)) / 2;

//    //    $('#parteneri').css({
//    //      left: l + 'px'
//    //    });

//    var tab = 0;
//    if ($('#BroadcastStreamMenu')) {
//      var dayOfWeek = (new Date).getDay() - 1;
//      tab = dayOfWeek;
//    }
//    if ($('.SelectableMenu')) {
//      switchTab(tab);
//      $('.SelectableMenu li a').each(function(i) {
//        $(this).click(function() {
//          switchTab(i);
//          return false;
//        });
//      });
//    }
//    if ($('#ProgSchedulleBtn')) {
//      switchDay(0);
//      $('#ProgSchedulleBtn li a').each(function(i) {
//        $(this).click(function() {
//          switchDay(i);
//          return false;
//        });
//      });
//    }
//  });



function switchTab(pageId) {
  $('.TextPanels').hide();
  $('.TextPanels:eq(' + pageId + ')').fadeIn('slow');
  $('.SelectableMenu li a').removeClass('SelectedMenuItem');
  $('.SelectableMenu li a:eq(' + pageId + ')').addClass('SelectedMenuItem');
  return false;
}

function switchDay(dayId) {
  $('.ProgSchedulleDay').hide();
  $('.ProgSchedulleDay:eq(' + dayId + ')').fadeIn('slow');
  $('#ProgSchedulleBtn li a').removeClass('Selected');
  $('#ProgSchedulleBtn li a:eq(' + dayId + ')').addClass('Selected');
  return false;
}

/*(function($){
$.fn.tooltip = function(options) {
var defaults = {
xOffset :  5,
yOffset : 10
};
var options = $.extend(defaults, options);
return this.each(function() {
$(this).hover(function(e){
if(this.title!="") {
this.t = this.title;
this.title = "";
$("body").append("<p id='tooltip'>"+ this.t +"</p>");
$("#tooltip")
.css({
position:   'absolute',
border:     '1px solid #333',
background: '#f7f5d1',
padding:    '2px 5px',
color:      '#333',
display:    'none'
})
.css("top",(e.pageY + options.yOffset) + "px")
.css("left",(e.pageX + options.xOffset) + "px")
.fadeIn(400);
}
},
function(){
if(typeof this.t != 'undefined') {
this.title = this.t;
$("#tooltip").remove();
}
});
$(this).mousemove(function(e){
$("#tooltip")
.css("top",(e.pageY + options.yOffset) + "px")
.css("left",(e.pageX + options.xOffset) + "px");
});
});
};
})(jQuery);*/
















///UCACAKLAR 


// scroll
var scrollWorking = false;
function block(id) {

  if (!scrollWorking) {
    // check scroll working
    scrollWorking = true;

    // scroll
    $('#site').scrollTo('#block_' + id, {
      duration: 1000,
      axis: 'x',
      onAfter: function () { scrollWorking = false; }
    });

    // check left
    if (id > 1 && $('#prev').is(':hidden')) {
      $('#prev').fadeIn('fast');
    } else if (id == 1) {
      $('#prev').fadeOut('fast');
    }

    // check right
    if (id < 4 && $('#next').is(':hidden')) {
      $('#next').fadeIn('fast');
    } else if (id == 4) {
      $('#next').fadeOut('fast');
    }

    // onClick set
    if (id == 1) {
      $('#next').unbind('click').click(function () { block(2); });

      // lang
      if (lang == 'ro') {
        $('#prev').attr('href', '#acasa');
        $('#next').attr('href', '#despre');
      } else {
        $('#prev').attr('href', '#home');
        $('#next').attr('href', '#about');
      }

    } else if (id == 2) {

      $('#prev').unbind('click').click(function () { block(1); });
      $('#next').unbind('click').click(function () { block(3); });

      // lang
      if (lang == 'ro') {
        $('#prev,#next').attr('href', '#despre');
      } else {
        $('#prev,#next').attr('href', '#about');
      }

    } else if (id == 3) {

      $('#prev').unbind('click').click(function () { block(2); });
      $('#next').unbind('click').click(function () { block(4); });

      // lang
      if (lang == 'ro') {
        $('#prev,#next').attr('href', '#proiecte');
      } else {
        $('#prev,#next').attr('href', '#projects');
      }

    } else if (id == 4) {

      $('#prev').unbind('click').click(function () { block(3); });
      $('#next').attr('href', '#contact');

      // lang
      if (lang == 'ro') {
        $('#prev').attr('href', '#proiecte');
      } else {
        $('#prev').attr('href', '#projects');
      }

    }
  }

}

// project list
var plistScroll = false;
function plist(id) {

  if (!plistScroll) {
    plistScroll = true;

    var nrDivs = $('#project_list').children().size();
    nrDivs = parseInt(nrDivs) - 1;

    if (id > 1 && $('#button-left').is(':hidden'))
      $('#button-left').fadeIn('fast');
    if (id > 1 && id <= (nrDivs - 2))
      $('#button-left').unbind('click').click(function () { plist((id - 1)); });
    else
      $('#button-left').fadeOut('fast');

    if (id >= (nrDivs - 2) && $('#button-right').is(':visible'))
      $('#button-right').fadeOut('fast');
    else {
      $('#button-right').fadeIn('fast');
      $('#button-right').unbind('click').click(function () { plist((id + 1)); });
    }

    $('#project_big').scrollTo('#plist_' + id, {
      duration: 1000,
      axis: 'x',
      onAfter: function () { plistScroll = false; }
    });

  }

}


///UCACAKLAR END
