var i=0;
function getUniqRandom(collection){
  var taken = "@";
  var maxCount = 9;
  for (var v = Math.floor(Math.random()*maxCount+1); taken.indexOf("@"+v+"@")!=-1; v = Math.floor(Math.random()*maxCount+1));
  taken += v+"@";
  return collection + '/' + v + 't.jpg';
}
 
function getThumbs() {
 var str = '\
  <span id="thumbImages1">';for (var i=3;i;i--) {str+='<a href="./shield/"><img src="./images/jewellery/'+getUniqRandom('shield')+'" alt="" /></a>'}str +='</span>\
  <span id="thumbImages2">';for (var i=3;i;i--) {str+='<a href="./frida/"><img src="./images/jewellery/'+getUniqRandom('frida')+'" alt="" /></a>'}str +='</span>\
  <span id="thumbImages3">';for (var i=3;i;i--) {str+='<a href="./caribou/"><img src="./images/jewellery/'+getUniqRandom('caribou')+'" alt="" /></a>'}str +='</span>\
  <span id="thumbImages4">';for (var i=3;i;i--) {str+='<a href="./ocean/"><img src="./images/jewellery/'+getUniqRandom('ocean')+'" alt="" /></a>'}str +='</span>';
 var imageThumbs = document.getElementById('imageThumbs');
 if (imageThumbs) imageThumbs.innerHTML=str;
}

if (window.$) {
	$(document).ready( function() {
	 var Container = document.getElementById('Container');
	 if(Container) {
		var splash = document.getElementById('BodySplash');
	   	if(splash) Container.style.height=getRelativeHeight(255);
	  	else Container.style.height=getRelativeHeight(264);
	 }


	$('#webForm').submit(function () {if(validateForm()) {$.post("./submit/", $("#webForm").serialize())};return false;});
	getThumbs();
		if (window.jcycle){
				$.fn.cycle.defaults = {
				fx:'fade',
				timeout:16000,
				speed:1000,  
				sync:1
				};


				$('#splashImages').cycle({delay:0,timeout:6000});
				$('#thumbImages2').cycle({delay:-9000});
				$('#thumbImages4').cycle({delay:-5000});
				$('#thumbImages1').cycle({delay:-1000});
				$('#thumbImages3').cycle({delay:3000});
				}

		if (window.jcarousel){
					var itemsSetSize = $('ul#mycarousel > li').size();		// Total number of items.
					var middleItemIndex = Math.round( itemsSetSize / 2 );	// Index of the middle item.
					var thisthis;
					function mycarousel_initCallback(carousel, state) {
						if (state == 'init') {
							var AllItems = $('ul#mycarousel > li');
							var FirstItem = AllItems.eq(0);
							var firstLargePath = FirstItem.children('a').attr('href');
							var firstLargeAlt = FirstItem.children('a').attr('title');
							var LargeImage = $('div#largeImage');
							var timer1;var timer2;
							FirstItem.addClass('jcarousel-item-current');		// Initial highlighting of the first item.
							$(this).children('img').attr({ src: firstLargePath, alt: '' }); // Initial large image assignment from the first item.
							
							AllItems.eq(middleItemIndex-1).after("<li style=\"font-size:1px;clear:left;float:none;display:block;width:1px;height:1px;overflow:hidden;margin:-13px 0 0 0; padding:0; background:0\"><\/li>");
							
							$('ul#mycarousel a').click( function() {
								clearTimeout(timer1);
								timer1 = null;
								clearTimeout(timer2);
								timer2 = null;
								$("#blinkingText").hide();
								// Get current Path and Alt for the large image.
									var currentLargePath = $(this).attr('href');
									var currentLargeAlt = $(this).attr('title');
								
								// Set class for current item to highlight it.
								AllItems.removeClass('jcarousel-item-current');
								$(this).parent('li').addClass('jcarousel-item-current');
								
								// Replace attributes for current image, toggling it.

								LargeImage.fadeOut(450, function() {
								$("#blinkingText").hide();
								timer1 = setTimeout(function(){$("#blinkingText").show();
								timer2 = setInterval(function(){$("#blinkingText").toggle();},450)},500);
								$(this).children('img').attr({ src: currentLargePath, alt: currentLargeAlt, id: 'jqi' });
								thisthis = this;
								} );
							return false;
							} );
						}


					      $("#jqi").bind("load", function() {
						$(thisthis).fadeIn(475);
							clearTimeout(timer1);
							timer1 = null;
							clearTimeout(timer2);
							timer2 = null;
							$("#blinkingText").fadeOut(400);
					      });
					}

					$('ul#mycarousel').jcarousel( {
						initCallback: mycarousel_initCallback,
						
						size: middleItemIndex, // There are 2 lines of items, so there are as many positions in Carousel, as half of the total number of "li" items.
						visible: 3,
						scroll: 1,	// Must be 1, due to 2 lines of items layout solution.
						animation: 'slow',
						auto: 0
					} );
				  	}
					$("#mycarousel li").each(function() {$("#mycarousel li").css({width: ""});});
					$("#mycarousel li a:eq("+i+")").each(function() {loadThem(this);});});
}


function loadThem(img){
	$("#mycarousel li a:eq("+i+")").each(function() {$("#preLoad").append('<img onload="loadThem(this)" src="'+$(this).attr('href') + '" alt="" />' )});
	i++;
}

function formSuccess() {
   var submit = document.getElementById('submit');
   if (submit) submit.disabled=true;
   var formArea = document.getElementById('formArea');
   if (formArea) formArea.style.visibility='hidden';

    $("#pageHeading").html('Thank You. We will respond promptly. <span>If you would like immediate help, call (902) 492-2947.</span>');
    return false;
}

function validateForm() {
    var error = '';
    var name = document.getElementById('name');
    if (name) {
        if (! (/[a-z]/i.test(name.value))) {
            name.style.backgroundColor = '#4F0F0F';
            error = error + 'You must provide your name.\n';
        } else name.style.backgroundColor = '#181818';
    }
    var email = document.getElementById('email');
    if (email) {
        if (/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(email.value) == false) {
            email.style.backgroundColor = '#4F0F0F';
            error = error + 'You must provide a valid email address.\n';
        } else email.style.backgroundColor = '#181818';
    }

   var details = document.getElementById('details');
   if (details) {
     if (!(/[a-z]/i.test(details.value))) {
            details.style.backgroundColor = '#4F0F0F';
            error = error + 'You must provide us with details about the project.\n';
       } else details.style.backgroundColor = '#181818';
    }
    var comments = document.getElementById('comments');
   if (comments) {
     if (! (/[a-z]/i.test(comments.value))) {
            comments.style.backgroundColor = '#4F0F0F';
            error = error + 'You must provide a question or comment.\n';
        } else comments.style.backgroundColor = '#181818';
    }
    if (error) { 
        alert(error);
        return false;
    } else {
	formSuccess();
        scroll(0, 0);
        return true;
    }
}


function getRelativeHeight(offset) {
  var myHeight = 0;
  if( typeof( window.innerHeight ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return (myHeight - offset)+'px';
}

