/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */





(function($,win,doc){
    
    
    
   var noreplaceUrl = false;
   $.replaceUrl=function(url){
       try{
           if(win.history.state!=url){
               var lurl = url.split(win.location.host)[1];
               win.history.pushState(lurl, null,lurl)
           }
           return true;
       }
       catch(e){
           // router ie fix
           var hrefs= url.split(win.location.host)[1];

           win.location.hash = hrefs == '/'?'':('!'+hrefs);
           last_hash  = win.location.hash;
           return false;
       }
       return false;
      
   } 
   
   
    $.checkPaths=function(fromlinks,url){
       
      var loc = url?url:doc.location.href;
      var pars = loc.split(win.location.host)[1]
      if(!win.history.pushState){
          var pars_m = loc.split(win.location.host)[1].match(/#!?(.*)/)
          if(pars_m){
            pars    = pars_m[1];
          }
          
      }else {
          pars = pars.replace(/#!?\/?/,'')
      }
      var $navs = $(fromlinks)
      var paired= false
      $navs.each(function(){
          var href = $(this).attr('href')
          if(href !== undefined && href.match(pars+'$')){
              $(this).trigger('mouseover');
              $(this).trigger('click');
              paired = true
              return false;
          }else if(href == undefined){
              
          } 
          return true;
       })
       if(!paired && url){
        var options = {fromSection:"#content",toSection:"#content", mode:"normal"};
        last_hash = url;
        $("#main-nav li.current-nav").removeClass('current-nav')
        $.replaceUrl(url)
        $.ajax({url:url,success:function(data){
               $(options.toSection).fadeOut(300,function(){
                   var title_mch=data.match(/\<title\>(.*)\<\/title\>/)
                   if(title_mch){
                         doc.title=title_mch[1]
                   }
                   var body_mch=data.match(/\<body\s+id\s?=\s?"?'?([\w\-0-9]+)'?"?/)
                   if(body_mch){
                         $('body').attr('id',body_mch[1]);
                   }
                   $(options.toSection).html($(options.fromSection,data).html())
                   $(options.toSection).fadeIn();
                   $(options.toSection).toAjax(options)
               }) 
            }});
       }
       return true
   } 
   
    
    
    $.fn.toAjax = function(opts) {
        
        var options = $.extend({fromSection:"#content",toSection:"#content", mode:"normal"},opts)
        var $last   = null;

        return $(this).each(function(){

            var $link = $(this)
            if($link.attr('ajax') == 'enable'){
                return true;
            }
            
            $link.attr('ajax','enable');
            $link.click(function(){
                 var url = $(this).attr('href');
                 
                 if(url.indexOf('http://')==-1){
                     url = 'http://'+win.location.host+url;
                 }
                 if(url.indexOf(win.location.host) == -1) {
                     return true
                 }
                 if(options.mode == 'normal'){
                     if($(this).parent().hasClass('current-nav')){
                         return false;
                     }
                     $(document.body).css('background-image','')
                     $link.parents('ul').find('.current-nav a')
                          .animate({color:'#bbb'},200,function(){
                             $(this).parent().removeClass('current-nav') 
                          })
                     $link.animate({color:'#fff'},200)
                     $(this).parent().removeClass('current-nav')
                     $(this).parent().addClass('current-nav')
                 }
                 
                                      
                 if(options.mode == 'press'){
                     if($link.hasClass('current')){
                         return false;
                     }
                     
                     $last = $link.parents('.press-list').find('.current');
                     $last.animate({color:'#666'},400);
                     $last.removeClass('current');
                     $link.animate({color:'#f80000'},400)
                     $link.addClass('current')
                 }
                 if(options.mode == 'agenda' || options.mode == 'gallery'){
                     if($link.hasClass('current')){
                         return false;
                     }
                     $last = $link.parents('.'+options.mode+'-list').find('.current');
                     $last.animate({color:'#666'},400);
                     $last.removeClass('current');
                     $link.animate({color:'#f80000'},400)
                     $link.addClass('current')
                 }
                 
                 $.ajax({url:url,
                     beforeSend: function( xhr ) {
                        xhr.overrideMimeType( 'text/plain; charset=utf-8' );
                        $('#ajaxloader').fadeTo(1000,1)
                     },
                     success:function(data){
                         $('#ajaxloader').stop().fadeOut(1000)
                         var title_mch=data.match(/\<title\>(.*)\<\/title\>/)
                         if(title_mch){
                             doc.title=title_mch[1]
                         }

                        if(options.mode == 'normal') {
                             $('.article',options.toSection).hide();
                             $(options.toSection).fadeOut(300,function(){

                                 var body_mch=data.match(/\<body\s+id\s?=\s?"?'?(\w+)'?"?/)
                                 if(body_mch){
                                     $('body').attr('id',body_mch[1]);
                                 }
                                 $(options.toSection).empty();
                                 $(options.toSection).hide()
                                 $(options.toSection).html($(options.fromSection,data))
                                 $('.article',options.toSection).hide();
                                 $(options.toSection).fadeIn(300,function(){
                                     $('.article',options.toSection).fadeIn(500);
                                     $(win).trigger('ajaxcomplate')
                                 })

                             })
                        }
                        else if(options.mode == 'press' || options.mode == 'agenda' || options.mode == 'gallery') {
                             $('.article',options.toSection).hide();
                             $(options.toSection).fadeOut(300,function(){

                                 $(options.toSection).empty();
                                 $(options.toSection).hide()
                                 
                                 $(options.toSection).html($(options.fromSection,data).html())
                                 $(options.toSection).fadeIn(300,function(){
                                     $(win).trigger('ajaxcomplate')
                                 })
                                 if($(".print-icon").length>0){
                                     $(".print-icon").attr("href",$(".print-icon",data).attr("href"))
                                 }
                                 if(options.mode == 'gallery'){
                                     if($link.hasClass('forpress')){
                                         $('.gallery-details').addClass('forpress')
                                     }
                                     else {
                                         $('.gallery-details').removeClass('forpress')
                                     }
                                 }
                             })
                        }
                        else if(options.mode == 'records'){
                            $('.record-title span').hide().html($('.record-title span',data).html());
                            $('.record-image').html($('.record-image',data).html())
                            $('.record-content').hide().html($('.record-content',data).html());
                            $('.record-summary').hide().html($('.record-summary',data).html());
                            $(win).trigger('ajaxcomplate')
                        }
                        $('#langmenu').html($('#langmenu',data).html())
                 }})
                $.replaceUrl(url)
                return false
            })
        })

    }
    
    $.fn.paginateScroll = function(){
        var $parent = $(this);
        if($parent.attr('paginate') == 'enabled')
            {return false}
        $parent.attr('paginate','enabled');
        var items = $parent.find('a');
        items.each(function(){
            var $item = $(this);
            $item.click(function(){
                var indexno = $item.index();
                $('.press-scroll').stop().animate({left:indexno*170*-1},600,'easeOutCubic')
                 $parent.find('.current').removeClass('current')
                 $item.addClass('current')
                return false;
            })
        })
        var indnum = $('.press-scroll').find('a.current')
        if(indnum.length){
            items.eq(indnum.parents('ul').index()).trigger('click');
        }
        
        $('.press-pagination, .press-list').bind('mousewheel',function(e,d){
            if(d<0){
                var $next = $parent.find('a.current').next()
                if( $next.length > 0 )
                    $next.trigger('click');
            }
            else if(d>0){
                var $prev = $parent.find('a.current').prev()
                if( $prev.length > 0 )
                    $prev.trigger('click');                
            }
            return false;
        })       
    }
    
    $.fn.recordsHover = function(){
        var $parent = $(this);
        $parent.attr('hover','enabled');
        var $current = $parent.find('a.current');
        
        
        $(this).find('a').each(function(){
            $(this).not('.current').animate({opacity:0.5})
            
            $(this).hover(function(){
                if($(this).hasClass('current')){return false}
                return $(this).stop()
                    .animate({'border-color':'#f80000',opacity:1},300)
            },function(){
                if($(this).hasClass('current')){return false}
                return $(this).stop()
                    .css({'border-color':'#f80000',opacity:1})
                    .animate({'border-color':'white',opacity:0.5},300)
            })
            
            $(this).click(function(){
                var $last = $parent.find('a.current').not(this);
                $last.removeClass('current');
                $last.trigger('mouseleave')
                $(this).addClass('current');
            })
                
            if(!$current.length){
                var dfurl = doc.location.href.match(/\/([a-zA-Z0-9\-\+\_]+)$/)[1];
                var tfurl = this.href.match(/\/([a-zA-Z0-9\-\+\_]+)$/)[1];
                if(tfurl == dfurl){
                    $(this).trigger('mouseover');
                    $(this).trigger('click');
                    $current = $(this);
                }
            }
        })
        if(!$current.length && !win.ajaxcomplaterecall){
            $(this).find('a').first().trigger('mouseover').trigger('click');
        }
    }
    
    $.fn.effectAlbum = function(){
        var div0 = $(this);
        var div1 = $('<div />');
        var div2 = $('<div />');
        var div3 = $('<div />');
        var imgc = div0.find('img').clone();
        
        div0.find('img').remove();
        
        div0.css({borderColor:'transparent',position:'relative'});
        div1.css({margin:-1,borderLeft:'1px solid silver',borderTop:'1px solid silver',position:'absolute',left:0,top:0})
        div2.css({margin:-1,borderRight:'1px solid silver',borderBottom:'1px solid silver',position:'absolute',right:0,bottom:0})
        div3.css({margin:0,'background-color':'#000',width:'100%', height:'100%'})
        
        $(this).append(div1);
        $(this).append(div2);
        $(this).append(div3);
        div1.animate({width:'100%',height:'100%'},600)
        div2.animate({width:'100%',height:'100%'},600)
        div3.animate({'background-color':'#fff'},300,'easeInBounce')
        div3.animate({'background-color':'#000'},300,function(){
            div0.css({borderColor:'silver'});
            div0.empty();
            div0.append(imgc)
            imgc.hide().fadeIn(600)
        })
    }
    
    $.fn.ufukbaharTrgeffect = function(opts){
        
        var options = $.extend({imgurl:""},opts)
        
        var $menu = $(this)
        
        var $trgimg = $('<img src="' + options.imgurl + '" id="trgimg" alt=""/>')
        
        $('body').append($trgimg);
        $('a',$menu).click(function(){
            
            var thw = $(this).width();
            var thl = $(this).offset().left;
            if($trgimg.is(':visible')){
                $trgimg.animate({left:thl+(thw/2)-4},{duration:500,easing:"easeOutExpo"})
            }
            else {
                $trgimg.css({top:0, left:thl+(thw/2)-4}).fadeIn(500)
            }
        })
        $('.current-nav a',$menu).trigger('click')
    }
    
    $.fn.hoverEfect = function(){
        var $tooltip = $('#tooltip');
        if($tooltip.length == 0){
            $tooltip = $('<div id="tooltip"></div>');
            $tooltip.appendTo(doc.body);
        }
        $tooltip.hide();
        var tt_tmo = null;
        
        
        $("#content").unbind('mousemove');
        $("#content").bind('mousemove',function(e){
            var mX = e.clientX;
            var mY = e.clientY;
            var mWidth = $tooltip.width();
            var mheight = $tooltip.height();
            if(mY-mheight-20<70){
                mY+= mheight+40;
            }
            if(mX-(mWidth/2)<20){
                mX = (mWidth/2) +20 -(35-mX*.25)
            }
            $tooltip.css({left:mX-(mWidth/2),top:mY-mheight-20});
            
        });
        
        
        return $(this).each(function(){
            var $rep = $(this)
            
            $rep.hover(function(){
                $tooltip.html($('span',$rep).html())
                $tooltip.hide();
                if(tt_tmo)clearTimeout(tt_tmo);
                if($tooltip.text().length>1)
                tt_tmo = setTimeout(function(){$tooltip.stop();$tooltip.fadeTo(500,1)},100);
                $(this).stop().animate({backgroundPosition:-100},200)
            },function(){
                if(tt_tmo)clearTimeout(tt_tmo);
                $tooltip.stop().fadeOut(200);
                $(this).stop().css({backgroundPosition:-100}).animate({backgroundPosition:-350},500)
            })
        })
    }    
    
    $.fn.sideRight = function(){
        var $this = $(this);
        var lix=0;
        for(var li = 0; li<$this.length; li++){
            var $linka = $('#main-nav a').eq(li);
            $linka.css({marginLeft:-40,opacity:0})
            setTimeout(function(){
                var $linka = $this.eq(lix);
                $linka.show();
                $linka.animate({marginLeft:0,opacity:1},1000,"easeOutCirc")
                    lix++;
            },60*li*((li+1)/8))
        }
    }
    
    $.fn.contactLayout = function (){
        var paragraphs = $(this).find('p')
        paragraphs.each(function(){
            var $prg = $(this);
            var ptop = $prg.offset().top
            $prg.css('padding-left',720-(ptop*(0.35)));
        })
    }
    
    var popuptmo = null;
    $.fn.addPrintIcon = function(content){
        var prnticon = $('<a href="/print" class="print-icon" target="blank" title="Print">Printable Version</a>')
        $(this).before(prnticon);
        prnticon.click(function(){
            this.href = $('.homepage a').attr('href')+'print/'+win.location.href.match(/[a-z0-9\-\+]+$/);
        })
    }
    
    $.ajaxPreload = function(){
        var url_reg = "\/([a-z]{2})\#!(.*)"
        var loc     = document.location.href
        if(loc.match(url_reg)){ // ie
            var regexec  = loc.match(url_reg);
            var simlurl  = regexec[2]
            var realurl   = 'http://'+document.location.host+simlurl
            var realpath  = 'http://'+document.location.host+simlurl.split('/')[0]+'/'+simlurl.split('/')[1]+'/'+simlurl.split('/')[2]
            var lastpath  = null;
            if($("#main-nav li.current-nav a").length){
                lastpath  = $("#main-nav li.current-nav a").attr('href').match(/[a-zA-Z0-9\-\+\_]+$/);
            }
            if(lastpath !== null && simlurl.split('/')[2] == lastpath){
                $.checkPaths("#content a",realurl);
            }
            else {
                $.checkPaths("#main-nav a",realpath);
                if(realpath != realurl){
                    win.ajaxcomplaterecall = function(){
                        win.ajaxcomplaterecall = function(){ $.checkPaths("#content a",realurl);}
                    }
                }
            }
        }
        else $.checkPaths("#main-nav a");
    }
    
    var checkWindowHash_tmo = null;
    var last_hash = null;
    var new_hash  = null;
    $.checkWindowHash = function(){
        if(checkWindowHash_tmo)clearTimeout(checkWindowHash_tmo);
        new_hash = window.location.hash;
        if(last_hash != new_hash){
            last_hash = new_hash;
            $.ajaxPreload();
        }
        checkWindowHash_tmo  = setTimeout($.checkWindowHash,100);
    }
    
    $.fullcheck = function(){
        if(screen.width == $(win).width() && screen.height == $(win).height()){
            $('.flloff').hide()
            $('.fllon').stop().fadeTo(500,1)
            
        }else {
            $('.fllon').hide()
            $('.flloff').stop().fadeTo(500,1)
        }
    }
    
    $.contentsize = function(){
        
            if($('.record-content').length){
                if($(win).height()<650){
                    $('.scrolled').css('height',$(win).height()-350)
                    $('.header-img').hide();
                }else {
                    $('.print-icon').css('top','')
                    $('.scrolled').css('height','');
                    $('.header-img').show();
                }
            }
            else if($('.scrolled').length){
                if($(win).height()-210<$('.scrolled').height()){
                    $('.scrolled').css('height',$(win).height()-200)
                    $('.print-icon').css('top',$('.scrolled').offset().top+$('.scrolled').height()+10)
                    $('.header-img').hide();
                }else {
                    $('.print-icon').css('top','')
                    $('.scrolled').css('height','');
                    $('.header-img').show();
                }
            }
        if($(win).height()<768){
            var rep_list_m_top = 140-(768-$(win).height())*.3;
            if(rep_list_m_top < 60)rep_list_m_top = 60;
            $('.rep-list').css('margin-top',rep_list_m_top)
            var rep_item_m_bottom = 8-(768-$(win).height())*.05;
            if(rep_item_m_bottom < -8 )rep_item_m_bottom = -8;
            $('.rep-item').css('margin-bottom',rep_item_m_bottom)
            
            $('.vertical-title').css({'margin-bottom':'-600px'})
            $('.vertical-title span').css({'font-size':'19px'})
            if($(win).height()<700)
                $('.vertical-title span').css({'font-size':'18px'})
            if($(win).height()<650)
                $('.vertical-title span').css({'font-size':'17px'})
            if($(win).height()<600)
                $('.vertical-title span').css({'font-size':'16px'})
            if($(win).height()<550)
                $('.vertical-title span').css({'font-size':'15px'})
            if($(win).height()<400)
                $('.vertical-title span').css({'font-size':'14px'})
        }else {
            $('.rep-list').css('margin-top','')
            $('.rep-item').css('margin-bottom','')
            $('.vertical-title span').css({'font-size':''})
            
        }
        
    }
    
})(jQuery,window,document)


function SelectContents(oid) {
    var elemToSelect = document.getElementById (oid);
    if (window.getSelection) {  // all browsers, except IE before version 9
        var selection = window.getSelection ();
        selection.selectAllChildren(elemToSelect);
    } else {                    // Internet Explorer before version 9
        var range = document.body.createTextRange ();
        range.moveToElementText (elemToSelect);
        range.select ();
    }
}



function RemoveSelection(oid) {
    var elemToSelect = document.getElementById(oid);
    if (window.getSelection) {  // all browsers, except IE before version 9
        var selection = window.getSelection ();  
        selection.selectAllChildren(elemToSelect);
        selection.removeAllRanges ();
    }
    else {
        if (document.selection.createRange) {        // Internet Explorer
            var range = document.selection.createRange ();
            document.selection.empty ();
        }
    }
}

function disableCtrlKeyCombination(e)
{
     //list all CTRL + key combinations you want to disable
     var forbiddenKeys = new Array('a');
     var key;
     var isCtrl;


    if(window.event)
     {
         key = window.event.keyCode;     //IE
         if(window.event.ctrlKey)
             isCtrl = true;
         else
             isCtrl = false;
         if(window.event.keyCode == 27){
             var scrolled = $(".scrolled")
             if(scrolled.length){
                     if(!scrolled[0].id){
                         scrolled[0].id="autoid_"+(new Date).valueOf();
                     }
                RemoveSelection(scrolled[0].id);
             }
             else if(document.getElementById('content'))
                RemoveSelection('content');
         }
     }
     else
     {
         key = e.which;     //firefox
         if(e.ctrlKey)
             isCtrl = true;
         else
             isCtrl = false;
     }

    //if ctrl is pressed check if other key is in forbidenKeys array
     if(isCtrl)
     {
         for(var i=0; i<forbiddenKeys .length; i++)
         {
             //case-insensitive comparation
             if(forbiddenKeys[i].toLowerCase() == String.fromCharCode(key).toLowerCase())
             {var scrolled = $(".scrolled")
                 if(scrolled.length){
                     if(!scrolled[0].id){
                         scrolled[0].id="autoid_"+(new Date).valueOf();
                     }
                    SelectContents(scrolled[0].id)
                 }
                 else if(document.getElementById('content'))
                     SelectContents('content')
                     return false;
             }
         }
     }
     return true;
}


