//$('.services-category.category, .portfolio-category.category, .services-entry.featured').css('background', '#FFA07A');

//http://htmlcssjavascript.com/javascript/javascript-parse-domain-name-out-of-a-string/
function getDomain (thestring) {
	if(thestring == ""){
		return false;
	}
	//simple function that matches the beginning of a URL
	//in a string and then returns the domain.
	var urlpattern = new RegExp("(http|ftp|https)://(.*?)/.*$");
	var parsedurl = thestring.match(urlpattern);
	return parsedurl[2];
}

(function($) {

	function pulse(elem, time){
		elem.fadeTo(time, 1, 'linear', function(){
			$(this).fadeTo(time, 0.05, 'linear', function(){
				pulse($(this), time);
			});
		});
	}

	var mobiles = {
		iPad: (navigator.userAgent.match(/iPad/i) != null) ? true : false,
		iPhone: (navigator.userAgent.match(/iPhone/i) != null) ? true : false,
		palm: (navigator.userAgent.match(/Palm/i) != null) ? true : false,
		blackberry: (navigator.userAgent.match(/Blackberry/i) != null) ? true : false,
		android: (navigator.userAgent.match(/Android/i) != null) ? true : false
	};

	function isMobile(){
		m = mobiles;
		if(m.iPad || m.iPhone || m.palm || m.blackberry || m.android){
			return true;
		} else {
			return false;
		}
	}

	var body = $('body');

	$('.resource-list a[href$=".pdf"]').addClass('pdf');

	var isPIE = false;
	if($.browser.msie && parseInt($.browser.version, 10) <= 8){
		isPIE = true;
	}

	if($.browser.safari){
		var userAgent = navigator.userAgent.toLowerCase();

		// Figure out what browser is being used
		jQuery.browser = {
			version: (userAgent.match( /.+(?:rv|it|ra|ie|me)[\/: ]([\d.]+)/ ) || [])[1],
			chrome: /chrome/.test( userAgent ),
			safari: /webkit/.test( userAgent ) && !/chrome/.test( userAgent ),
			opera: /opera/.test( userAgent ),
			msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
			mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
		};
	
		if($.browser.chrome){
			//remove text shadow class from html tag
			$('html').removeClass('textshadow');
			$('html').addClass('no-textshadow');
		}
	}

	var headerA = $('header h1 a');
	var html = $('html');
	if(html.hasClass('ie6') || html.hasClass('ie7') || html.hasClass('ie8')){
	} else {
		headerA.clone()
		.addClass('clone')
		.css('opacity', 0)
		.appendTo('header h1')
		.css('position', 'absolute')
		.hover(function(){
			$(this).stop().fadeTo(300, 1);			
		}, function(){
			$(this).stop().fadeTo(300, 0);		
		});
	}

	//make the footer sticky
	function positionFooter(){
		var doc = $(document); 
		var win = $(window);
		var html = $('html');
		var body = $('body');
		var footer = $('footer');
		var height = footer.outerHeight();
		footer.css('position', 'absolute');
		$('#container-end').css('marginBottom', 0);
		//test if the document height is greater than the html height
		//console.log(document.height(), html.height(), $('footer').position());
		//alert('win.height(): '+win.height()+"\n"+'html.height(): '+html.height()+"\n"+'doc.height(): '+doc.height());
		//alert('ie?: '+$.browser.msie);
		if(!$.browser.msie){		
			if((win.height()-height) > html.height()){
				//position the footer accordingly
				footer.css('top', win.height()-height);
			}
		} else {
			//alert('win.height(): '+win.height()+"\n"+'html.height(): '+html.height()+"\n"+'doc.height(): '+doc.height()+"\n"+'body.height(): '+body.height());
			if((doc.height()) > (body.height()+height)){
				//position the footer accordingly
				footer.css('top', doc.height() - height);
				//alert(doc.height()+', '+height);
			}
		}
		if(body.width() > 1080){
			if(!isMobile()){footer.width('100%');}
		} else {footer.width('1080px');}
	}

	//skip to for service entries
	$('.service-entry').filter('.business-cards, .postcards, .brochures-flyers').each(function(index){
		var sn = $('<div class="service-nav"><span>Skip To:</span></div>');
		var elem = $(this);
		elem.find('.service-description').find('a:not([href])').each(function(index){
			var anchor = $(this);
			sn.append('<a href="#'+anchor.attr('name')+'"><span>'+anchor.text()+'</span></a>');
		});
		elem.find('.discl').after(sn);
	});

	$('.service-entry').find('.service-description table').each(function(index){
		$(this).after('<span class="pricing-note">* Prices include GST, and not delivery.</span>');
	});


	$("a[rel='fancybox']").fancybox();
	
	var homeSlider = $('#home-slider');
	var homeSliderWrapper = homeSlider.find('.images');
	homeSlider.css('position', 'relative').addClass('slider');
	//homeSlider.append('<div class="pauseButton" style="display: block; width: 40px; height: 40px; z-index: 500; position: absolute; top: 128px; right: 140px; background: url(/assets/images/pause.png);" />');
	//var pauseButton = homeSlider.children('.pauseButton');
	//pauseButton.hide();

	homeSliderWrapper.nivoSlider({
		pauseTime: 8000,
		directionNav: false,
		pauseOnHover: false
	});
	$('#home-specials').nivoSlider({
		directionNav: false,
		controlNav: false,
		pauseTime: 8000,
		pauseOnHover: false,
		effect: 'fade'
		
	}).find('a').css('display', 'block');

	/*homeSliderWrapper.hover(function(ev){
		//console.log('mouse entered');
		if(isPIE){pauseButton.show();} else {
			pulse(pauseButton, 1000);
		}
	}, function(ev){
		//console.log('mouse left');
		if(isPIE){pauseButton.hide();} else {
			pauseButton.stop().fadeTo(400, 0);
		}
	});*/


	if(html.hasClass('ie7')){
		var control = $('#home-slider .nivo-controlNav');
		control.css('marginLeft', (-control.width())/2);		
	} else if (html.hasClass('ie8')){
		var control = $('#home-slider .nivo-controlNav');
		control.css('marginLeft', (760-control.width())/2);
	}

	options = {  
		reject : { // Rejection flags for specific browsers  
			all: false, // Covers Everything (Nothing blocked)  
			msie5: true,msie6: true // Covers MSIE 5-6 (Blocked by default)  
			/* 
				Possibilities are endless... 
	 
				msie: false,msie5: true,msie6: true,msie7: false,msie8: false, // MSIE Flags (Global, 5-8) 
				firefox: false,firefox1: false,firefox2: false,firefox3: false, // Firefox Flags (Global, 1-3) 
				konqueror: false,konqueror1: false,konqueror2: false,konqueror3: false, // Konqueror Flags (Global, 1-3) 
				chrome: false,chrome1: false,chrome2: false,chrome3: false,chrome4: false, // Chrome Flags (Global, 1-4) 
				safari: false,safari2: false,safari3: false,safari4: false, // Safari Flags (Global, 1-4) 
				opera: false,opera7: false,opera8: false,opera9: false,opera10: false, // Opera Flags (Global, 7-10) 
				gecko: false,webkit: false,trident: false,khtml: false,presto: false, // Rendering Engines (Gecko, Webkit, Trident, KHTML, Presto) 
				win: false,mac: false,linux : false,solaris : false,iphone: false, // Operating Systems (Win, Mac, Linux, Solaris, iPhone) 
				unknown: false // Unknown covers everything else 
			*/  
		},  
		display: ['firefox','chrome','msie','opera'], // What browsers to display and their order  
		browserInfo: { // Settings for which browsers to display  
			firefox: {  
				text: 'Firefox', // Text below the icon  
				url: 'http://www.mozilla.com/firefox/' // URL For icon/text link  
			},  
			opera: {  
				text: 'Opera',  
				url: 'http://www.opera.com/download/'  
			},  
			chrome: {  
				text: 'Google Chrome',  
				url: 'http://www.google.com/chrome/'  
			},  
			msie: {  
				text: 'Internet Explorer 9',  
				url: 'http://www.microsoft.com/windows/Internet-explorer/'  
			}
		},  
		header: 'Did you know that your Internet Browser is out of date?', // Header of pop-up window  
		paragraph1: 'Your browser is out of date, and may not be compatible with our website. A list of the most popular web browsers can be found below.', // Paragraph 1  
		paragraph2: 'Just click on the icons to get to the download page', // Paragraph 2  
		close: true, // Allow closing of window  
		closeMessage: 'By closing this window you acknowledge that your experience on this website may be degraded', // Message displayed below closing link  
		closeLink: 'Close This Window', // Text for closing link  
		closeURL: '#', // Close URL  
		closeESC: true, // Allow closing of window with esc key  
		closeCookie: false, // If cookies should be used to remmember if the window was closed (see cookieSettings for more options)  
		// Cookie settings are only used if closeCookie is true  
		cookieSettings: {  
			path: '/', // Path for the cookie to be saved on (should be root domain in most cases)  
			expires: 0 // Expiration Date (in seconds), 0 (default) means it ends with the current session  
		},  
		imagePath: '/assets/images/browsers/', // Path where images are located  
		overlayBgColor: '#000', // Background color for overlay  
		overlayOpacity: 0.8, // Background transparency (0-1)  
		fadeInTime: 'fast', // Fade in time on open ('slow','medium','fast' or integer in ms)  
		fadeOutTime: 'fast' // Fade out time on close ('slow','medium','fast' or integer in ms)  
	}
	$.reject(options);

	
/*
	var footer = $('footer');
	var animate = false;
	if(body.hasClass('home')){
		var referrer = getDomain(document.referrer);
		if(referrer == false || (referrer !== "drawn2design.com.au" && referrer !== "d2d.local")){
			var animate = true;
		}
	}
*/
	//add #fKnight div
	var footer = $('footer');
	footer.append('<div id="fknight"/>');
	body.addClass('fknight');
	if(html.hasClass('ie6') || html.hasClass('ie7')){$('footer').height('200px');}
	//alert(isMobile());
	$(window).load(function(){
		if(isMobile()){
			footer.css('display', 'block');
		} else {
			positionFooter();
			footer.fadeTo(1000, 1);
			$(window).resize(positionFooter);
		}
	});
	
	$('#google-map-1').css('display', 'block').css('width', '500').css('height', '300').children().hide();

	//$('#google-map-1').gMap();

	$('.services.browse > .featured>span').append('<img class="featured-image" style="width: 65px; height: 63px; position: absolute; top: -6px; left: -8px; display: block;" src="/assets/images/featured.png"/>');


 	var options = { 
		target:        '',   // target element(s) to be updated with server response 
		beforeSubmit:  function(){
			var form = $('#contact_form');
			form.append('<div class="loading" style="clear: both;"><p>Loading <img src="/assets/images/ajax-loader.gif"/></p></div>');
			form.find('input[type="submit"]').attr('disabled', 'disabled');
			form.find('.message.err').fadeOut().remove();
			
		},  // pre-submit callback 
		complete:       function(responseText, statusText){
			var form = $('#contact_form');
			form.find('.loading').fadeOut().remove();
			if(responseText.getResponseHeader('refresh') != null){
				//ajax was successful, inform user
				form.children().fadeOut();
				form.append('<p style="color: #0f0;">Success! We have received your email and will get back to you soon.</p>');
				//form.fadeOut();
			} else {
				var rt = responseText.responseText;
				var holster = $('<div/>').append(rt);
				var response = '';
				holster.find('ul li').each(function(){
					response += '<p style="color: #f00;"> - '+$(this).text()+'</p>';
				});
				form.find('input[type="submit"]').removeAttr('disabled');
				$('<div class="message err"><p style="color: #f00;">Error:</p>'+response+'</div>').appendTo(form);
			}
		}  // post-submit callback 
	}; 
 
	// bind form using 'ajaxForm' 
	$('#contact_form').ajaxForm(options).find('input[type="submit"]').removeAttr('disabled');

	var id = 'google-map-1';
	if($('#'+id).length > 0){
		var latlng = new google.maps.LatLng(-27.4363158, 153.1581563);
		var myOptions = {
			zoom: 16,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(document.getElementById(id), myOptions);
	
		var marker = new google.maps.Marker({
			position: latlng, 
			map: map,
			title:"Drawn 2 Design"
		});
		google.maps.event.addListener(map, 'tilesloaded',function(){
			$('#'+id).css('overflow', 'visible');
		});
	}
	if($.browser.msie){
		$('#contact_form textarea').attr('rows', '7');
	}

var goToAnchor = function(anchor, complete){
	
	$('html,body').animate({
	    scrollTop: anchor.offset().top-30
	}, {
	    easing: 'linear',
	    duration: 1000,
	    complete: complete
	});
	
}

$('a').live('click', function(ev){
	var href = $(this).attr('href');
	if(href.substring(0,1) === '#'){
		var anchor = $('a[name="'+href.substring(1)+'"]')
		if(anchor.length){
			ev.preventDefault();
			goToAnchor(anchor, function(){
				var parent = anchor.parents('.col').first();
				parent.addClass('flash');
				setTimeout(function(){parent.removeClass('flash');}, 800);
			});
		}
	}
});


	
})(jQuery);
