

/* Copyright (c) 2008 Kean Loong Tan http://www.gimiti.com/kltan
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * Copyright notice and license must remain intact for legal use
 * jFade
 * Version: 1.0 (Jun 30, 2008)
 * Requires: jQuery 1.2.6+
 * 
 *
 * Original Code Copyright (c) 2008 by Michael Leigeber
 * Website: http://www.leigeber.com
 * 
 *
 */ 
(function(A){A.fn.jFade=function(N){var B=A.extend({},A.fn.jFade.defaults,N);var F,C,K,E,H,J,I,M,D;var G=this;var L=function(){var Q=G;B.steps=B.steps||20;B.duration=B.duration||20;clearInterval(Q.timer);C=O(B.end);K=C[0];E=C[1];H=C[2];if(!Q.r){F=O(B.start);r=F[0];g=F[1];b=F[2];Q.r=r;Q.g=g;Q.b=b}J=Math.round(Math.abs(Q.r-K)/B.steps);I=Math.round(Math.abs(Q.g-E)/B.steps);M=Math.round(Math.abs(Q.b-H)/B.steps);if(J==0){J=1}if(I==0){I=1}if(M==0){M=1}Q.step=1;Q.timer=setInterval(function(){P(Q,B.property,B.steps,K,E,H,J,I,M)},B.duration);function P(V,d,a,e,U,X,c,Y,f){var T=V;var S;if(T.step<=a){var R=T.r;var W=T.g;var Z=T.b;if(R>=e){R=R-c}else{R=parseInt(R)+parseInt(c)}if(W>=U){W=W-Y}else{W=parseInt(W)+parseInt(Y)}if(Z>=X){Z=Z-f}else{Z=parseInt(Z)+parseInt(f)}S="rgb("+R+","+W+","+Z+")";A(V).css(d,S);T.r=R;T.g=W;T.b=Z;T.step=T.step+1}else{clearInterval(T.timer);S="rgb("+e+","+U+","+X+")";A(V).css(d,S)}}function O(R){var S=[parseInt(R.substring(0,2),16),parseInt(R.substring(2,4),16),parseInt(R.substring(4,6),16)];return S}};if(B.trigger=="load"){L()}else{A(this).bind(B.trigger,function(){G=this;L()})}return this};A.fn.jFade.defaults={trigger:"load",property:"background",start:"FFFFFF",end:"000000",steps:5,duration:30}})(jQuery);
function badBrowser(){
    if($.browser.msie && parseInt($.browser.version) <= 6){ return true;}
    
    return false;
}

function getBadBrowser(c_name)
{
    if (document.cookie.length>0)
    {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1)
        { 
        c_start=c_start + c_name.length+1; 
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
        } 
    }
    return "";
}   

function setBadBrowser(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

if(badBrowser() && getBadBrowser('browserWarning') != 'seen' ){
    $(function(){
        $("<div id='browserWarning'>You are using an unsupported browser. Please switch to <a href='http://getfirefox.com'>FireFox</a> or <a href='http://www.apple.com/safari/'>Safari</a>&nbsp;&nbsp;&nbsp;[<a href='#' id='warningClose'>close</a>] </div> ")
            .css({
                backgroundColor: '#fcfdde',
                'width': '100%',
                'border-top': 'solid 1px #000',
                'border-bottom': 'solid 1px #000',
                'text-align': 'center',
                padding:'5px 0px 5px 0px'
            })
            .prependTo("body");
        
        $('#warningClose').click(function(){
            setBadBrowser('browserWarning','seen');
            $('#browserWarning').slideUp('slow');
            return false;
        });
    }); 
}

/*
 * jQuery Cycle Lite Plugin
 * http://malsup.com/jquery/cycle/lite/
 * Copyright (c) 2008-2011 M. Alsup
 * Version: 1.1 (03/07/2011)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * Requires: jQuery v1.3.2 or later
 */
;(function($){var ver="Lite-1.1";$.fn.cycle=function(options){return this.each(function(){options=options||{};if(this.cycleTimeout){clearTimeout(this.cycleTimeout);}this.cycleTimeout=0;this.cyclePause=0;var $cont=$(this);var $slides=options.slideExpr?$(options.slideExpr,this):$cont.children();var els=$slides.get();if(els.length<2){window.console&&console.log("terminating; too few slides: "+els.length);return ;}var opts=$.extend({},$.fn.cycle.defaults,options||{},$.metadata?$cont.metadata():$.meta?$cont.data():{});opts.before=opts.before?[opts.before]:[];opts.after=opts.after?[opts.after]:[];opts.after.unshift(function(){opts.busy=0;});var cls=this.className;opts.width=parseInt((cls.match(/w:(\d+)/)||[])[1])||opts.width;opts.height=parseInt((cls.match(/h:(\d+)/)||[])[1])||opts.height;opts.timeout=parseInt((cls.match(/t:(\d+)/)||[])[1])||opts.timeout;if($cont.css("position")=="static"){$cont.css("position","relative");}if(opts.width){$cont.width(opts.width);}if(opts.height&&opts.height!="auto"){$cont.height(opts.height);}var first=0;$slides.css({position:"absolute",top:0,left:0}).each(function(i){$(this).css("z-index",els.length-i);});$(els[first]).css("opacity",1).show();if($.browser.msie){els[first].style.removeAttribute("filter");}if(opts.fit&&opts.width){$slides.width(opts.width);}if(opts.fit&&opts.height&&opts.height!="auto"){$slides.height(opts.height);}if(opts.pause){$cont.hover(function(){this.cyclePause=1;},function(){this.cyclePause=0;});}var txFn=$.fn.cycle.transitions[opts.fx];txFn&&txFn($cont,$slides,opts);$slides.each(function(){var $el=$(this);this.cycleH=(opts.fit&&opts.height)?opts.height:$el.height();this.cycleW=(opts.fit&&opts.width)?opts.width:$el.width();});if(opts.cssFirst){$($slides[first]).css(opts.cssFirst);}if(opts.timeout){if(opts.speed.constructor==String){opts.speed={slow:600,fast:200}[opts.speed]||400;}if(!opts.sync){opts.speed=opts.speed/2;}while((opts.timeout-opts.speed)<250){opts.timeout+=opts.speed;}}opts.speedIn=opts.speed;opts.speedOut=opts.speed;opts.slideCount=els.length;opts.currSlide=first;opts.nextSlide=1;var e0=$slides[first];if(opts.before.length){opts.before[0].apply(e0,[e0,e0,opts,true]);}if(opts.after.length>1){opts.after[1].apply(e0,[e0,e0,opts,true]);}if(opts.click&&!opts.next){opts.next=opts.click;}if(opts.next){$(opts.next).bind("click",function(){return advance(els,opts,opts.rev?-1:1);});}if(opts.prev){$(opts.prev).bind("click",function(){return advance(els,opts,opts.rev?1:-1);});}if(opts.timeout){this.cycleTimeout=setTimeout(function(){go(els,opts,0,!opts.rev);},opts.timeout+(opts.delay||0));}});};function go(els,opts,manual,fwd){if(opts.busy){return ;}var p=els[0].parentNode,curr=els[opts.currSlide],next=els[opts.nextSlide];if(p.cycleTimeout===0&&!manual){return ;}if(manual||!p.cyclePause){if(opts.before.length){$.each(opts.before,function(i,o){o.apply(next,[curr,next,opts,fwd]);});}var after=function(){if($.browser.msie){this.style.removeAttribute("filter");}$.each(opts.after,function(i,o){o.apply(next,[curr,next,opts,fwd]);});};if(opts.nextSlide!=opts.currSlide){opts.busy=1;$.fn.cycle.custom(curr,next,opts,after);}var roll=(opts.nextSlide+1)==els.length;opts.nextSlide=roll?0:opts.nextSlide+1;opts.currSlide=roll?els.length-1:opts.nextSlide-1;}if(opts.timeout){p.cycleTimeout=setTimeout(function(){go(els,opts,0,!opts.rev);},opts.timeout);}}function advance(els,opts,val){var p=els[0].parentNode,timeout=p.cycleTimeout;if(timeout){clearTimeout(timeout);p.cycleTimeout=0;}opts.nextSlide=opts.currSlide+val;if(opts.nextSlide<0){opts.nextSlide=els.length-1;}else{if(opts.nextSlide>=els.length){opts.nextSlide=0;}}go(els,opts,1,val>=0);return false;}$.fn.cycle.custom=function(curr,next,opts,cb){var $l=$(curr),$n=$(next);$n.css(opts.cssBefore);var fn=function(){$n.animate(opts.animIn,opts.speedIn,opts.easeIn,cb);};$l.animate(opts.animOut,opts.speedOut,opts.easeOut,function(){$l.css(opts.cssAfter);if(!opts.sync){fn();}});if(opts.sync){fn();}};$.fn.cycle.transitions={fade:function($cont,$slides,opts){opts.cssBefore={opacity:0};opts.animOut={opacity:0};opts.animIn={opacity:1};}};$.fn.cycle.ver=function(){return ver;};$.fn.cycle.defaults={fx:"fade",timeout:4000,speed:1000,next:null,prev:null,before:null,after:null,height:"auto",sync:1,fit:0,pause:0,delay:0,slideExpr:null,cssBefore:{},cssAfter:{},animIn:{},animOut:{}};})(jQuery);

var isiPad = navigator.userAgent.match(/iPad/i) != null;
var isiPhone = navigator.userAgent.match(/iPhone/i) != null;

var slides = $('#slider img');
var totalSlides = slides.length;
var activeSlide = 0;
var leftOffset = 0;
var windowWidth = $(window).width();

/** CUSTOM SCRIPTS **/
$(document).ready(function(){

	slides = $('#slider img');
	totalSlides = slides.length;
	activeSlide = 0;
	leftOffset = 0;
	windowWidth = $(window).width();

	// iPad warning
	if(isiPad){
		iPadWarning = "<p style='text-transform:uppercase;padding-top:10px;font-size:.9em;line-height:1.2em;'>This site is not yet fully optimised for the iPad.<br/>Please <a href='http://boraaksu.us2.list-manage.com/subscribe?u=f9678f1cdf5835311086b667e&id=9e07f367ef' target='_blank'>sign up</a> for updates.</p>";
		$('#paper').append(iPadWarning);
	} else if(isiPhone) {
		iPhoneWarning = "<p style='text-transform:uppercase;padding-top:10px;font-size:.9em;line-height:1.2em;'>This site is not yet fully optimised for the iPhone.<br/>Please <a href='http://boraaksu.us2.list-manage.com/subscribe?u=f9678f1cdf5835311086b667e&id=9e07f367ef' target='_blank'>sign up</a> for updates.</p>";
		$('#paper').append(iPhoneWarning);
	}

	/* Temp disable shop/explore
	$('#sections a').click(function(){
		if($(this).attr('href') == 'http://explore.boraaksu.com')
		{
			alert('Coming soon!');
			return false;
		}
	});*/

	// Show details
	$('div.fadein')
		.css({ opacity:0 })
		.delay(700)
		.each(function(i){
			$(this).delay(i*250).animate({ opacity : 1 }, 350);
		});
	
	// Form AJAX
	$('#loading').hide();
	$('#subscribe').submit(function(){
		$('#loading').show();
		$('#email').blur();
		form = $(this);
		$.post(form.attr('action'), $(form).serialize(),
        function(data){
        	$('#loading').hide();
        	if(data.result == 'success'){
        		$('#email').val('');
        	} else{
        		$('#email').focus();
        	}
            $('#message').html(data.message);
        }, 'json');
		return false;
	});
	
    // Home Images
    resizeImage($('#bg'));
    $(window).resize(function(){resizeImage($('#bg'));});
    
    // Menu
    pauseHover = false;
    $('nav#top-level').hover(function(){
		if(!pauseHover) $(this).addClass('hover');
	}, function(){
		pauseHover = false;
		$(this).removeClass('hover');
	});
	
	$('a[href=#overlay]').click(function(){
		$('#overlay').fadeIn(300);
		return false;
	});
	$('a#close').click(function(){
		$('#overlay').fadeOut(300);
		pauseHover = true;
		return false;
	});
	
	// Slideshow
	slides.each(function(){
		var newHeight = 400;
		var newWidth = ($(this).attr('width')/$(this).attr('height'))*newHeight;
				
		$(this).css({width:newWidth, height:newHeight});
		$(this).data({width:newWidth, height:newHeight});	
	});
			
	initSlider(activeSlide);
	showCaption(slides.eq(0));
	
	$(window).resize(function(){
		initSlider(activeSlide);
	});
	
	$('#slider, #next').click(function(){
		moveSlide();
		return false;
	});
	
	$('#prev').click(function(){
		moveSlide(true);
		return false;
	});
	    
});

function resizeImage(image)
{
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var imageWidth = image.attr('width');
	var imageHeight = image.attr('height');

	if(imageWidth/imageHeight > windowWidth/windowHeight)
	{
		// Fit to height, crop horizontally
		var newHeight = windowHeight;
		var newWidth = (imageWidth/imageHeight)*newHeight;
		var topOffset = 0;
		var leftOffset = (windowWidth-newWidth)/2;
	}
	else
	{
		// Fit to width, crop vertically
		var newWidth = windowWidth;
		var newHeight = (imageHeight/imageWidth)*newWidth;
		var topOffset = (windowHeight-newHeight)/2;
		var leftOffset = 0;
	}
	image.css({
		height: newHeight,
		width:	newWidth,
		marginTop:	topOffset,
		marginLeft:	leftOffset
	});
}

function initSlider(position)
{
	windowWidth = $(window).width();
	
	if(position)
	{
		newOffset = (windowWidth/2);
		slides.each(function(i){
			if(i < activeSlide) {
				newOffset = newOffset - 20 - (slides.eq(i).data('width'));
			} else if(i == activeSlide) {
				newOffset = newOffset - (slides.eq(i).data('width')/2);
			}
		});
		leftOffset = newOffset;
		$('#slider').css({left:newOffset});
	}
	else
	{
		leftOffset = (windowWidth/2)-(slides.eq(0).data('width')/2);
		$('#slider').css({left:leftOffset});
	}
}

function showCaption(slide)
{
	clickThrough = slide.attr('data-url');
	caption = slide.attr('alt');
	$('#caption').fadeOut(100, function(){
		if(clickThrough != '') {
			$('#caption').html('<a href="' + clickThrough + '" target="_blank">' + caption + '</a>');
		} else {
			$('#caption').html(caption);
		}
		$('#caption').fadeIn(200);
	});
}

function moveSlide(reverse)
{
	if(reverse)
	{
		if((activeSlide+1) > 1)
		{
			leftOffset = leftOffset + ((slides.eq(activeSlide).data('width')/2) + 20 + (slides.eq(activeSlide-1).data('width')/2));
			$('#slider').animate({left:leftOffset},300);
			activeSlide = activeSlide-1;
			showCaption(slides.eq(activeSlide));
			$('#current').text(activeSlide+1);
			return true;
		}
		else
		{
			// do nothing
			return true;
		}
	}
	else
	{
		if((activeSlide+1) < totalSlides)
		{
			leftOffset = leftOffset - ((slides.eq(activeSlide).data('width')/2) + 20 + (slides.eq(activeSlide+1).data('width')/2));
			$('#slider').animate({left:leftOffset},300);
			activeSlide = activeSlide+1;
			showCaption(slides.eq(activeSlide));
			$('#current').text(activeSlide+1);
			return true;
		}
		else
		{
			speed = 300 * (slides.length/3);
			leftOffset = (windowWidth/2)-(slides.eq(0).data('width')/2);
			$('#slider').animate({left:leftOffset}, speed);
			activeSlide = 0;
			showCaption(slides.eq(activeSlide));
			$('#current').text(activeSlide+1);
			return true;
		}
	}
}

