function attatchTB(){
  var userAgent = navigator.userAgent.toLowerCase();
  var browserVersion = (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1];
	var isIE6 = (/msie/.test(userAgent) && !/opera/.test(userAgent) && parseInt(browserVersion) < 7);
if (isIE6) {
   $('#portright a').fancybox({
    'frameWidth': 775,
    'frameHeight': 550,
    'overlayShow': false,
    'padding': 0
    });
} else {
   $('#portright a').fancybox({
    'frameWidth': 775,
    'frameHeight': 550,
    'overlayShow': true,
    'padding': 0
    });
} 
}
function attatchLeftTB(){
  var userAgent = navigator.userAgent.toLowerCase();
  var browserVersion = (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1];
  var isIE6 = (/msie/.test(userAgent) && !/opera/.test(userAgent) && parseInt(browserVersion) < 7);
if (isIE6) {
	$('#casestudy a').fancybox({
		'frameWidth': 800,
		'frameHeight': 550,
		'overlayShow': false,
		'padding': 0,
		'hideOnContentClick':true 
	});
} else {
   $('#casestudy a').fancybox({
    'frameWidth': 800,
    'frameHeight': 550,
    'overlayShow': true,
    'padding': 0,
		'hideOnContentClick':true 
    });
}

}
function portfolio(){
	attatchTB();
  $("#portleft li:first").addClass("selected");
  $("#portleft li").click(function(){
    $("li.selected").removeClass("selected");
    $(this).addClass("selected");
    var toLoad = 'portfoliodetails.html?cat='+$(this).attr('id');
    $('#portright').load(toLoad, function(){
			attatchTB();
     });
   return false;
   });
   $("#portright li a").click(function(){
   });
}


function initMenus() {
  $('ul.menu ul').hide();
  $.each($('ul.menu'), function(){
    var cookie = $.cookie(this.id);

    if(cookie === null || String(cookie).length < 1) {
      $('#' + this.id + '.expandfirst ul:first').show();
    }
    else {
      $('#' + this.id + ' #' + cookie).next().show();
    }
  });
  $('ul.menu li a').click(
    function() {

      var checkElement = $(this).next();
      var parent = this.parentNode.parentNode.id;

      if($('#' + parent).hasClass('noaccordion')) {
        if((String(parent).length > 0) && (String(this.id).length > 0)) {
          if($(this).next().is(':visible')) {
            $.cookie(parent, null);
          }
          else {
            $.cookie(parent, this.id);
          }
          $(this).next().slideToggle('normal');
        }
      }
      if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
        if($('#' + parent).hasClass('collapsible')) {
          $('#' + parent + ' ul:visible').slideUp('normal');
        }
        return;
      }
      if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
        $('#' + parent + ' ul:visible').slideUp('normal');
        if((String(parent).length > 0) && (String(this.id).length > 0)) {
          $.cookie(parent, this.id);
        }
        checkElement.slideDown('normal');
        return;
      }
    }
  );
}

function casestudy(){
  $('#cs_navigation a').click(function(){
    var toLoadRight = 'casestudydetails.html?x=right&cs='+$(this).attr('id')+'&sec='+$(this).attr('href');
    var toLoadLeft= 'casestudydetails.html?x=left&cs='+$(this).attr('id')+'&sec='+$(this).attr('href');
    $('#cs_ajax_right').load(toLoadRight);
    $('#cs_ajax_left').load(toLoadLeft);
    return false;
  });
}

function jqm(){
  $('#ex2').jqm({
      trigger: 'a.ex2trigger'
    });
}


$(document).ready(function() {
	attatchTB();
	attatchLeftTB();
	jqm();
  var hash = window.location.hash.substr(1);
  var href = $('a.acc').each(function()
  {
    var href = $(this).attr('href');
      if(hash==href.substr(0,href.length-5))
      {
        var toLoad = hash+'.html #main_content';
        $('#main_content').load(toLoad, function()
        {
          portfolio();
        });
      }
  });

  $('a.acc').click(function(){
  var toLoad = $(this).attr('href')+' #main_content';	
    $('#main_content').load(toLoad, function(){
			portfolio();	
			attatchTB();
  });

  window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
  return false;
  });

	portfolio();
	initMenus();
});
