$(document).ready(function(){
	initMenu();
	launchIframeLinks();
	scroller();
	customEnvelopes();
	sorting();
	emailFriend();
	clearTextboxes();
	initBanners();
	carousel();
	gallery();
	accordianMenu();
});

function initMenu(){
	$('#menu li').hover(
        function () {
			$(this).children('ul').css('display','none');
			$(this).children('ul').css('display','block');
        },function(){
			$(this).children('ul').css('display','none');
	});			
}

function launchIframeLinks(){
	$('#sidemenu ul a').click(function(){
		window.parent.location = $(this).attr('href');							
	});
}

function scroller(){
	$('#scroller li').each(function(i){
		if((i%4) == 3){
			$(this).css('margin-right','0');	
		}
	});
}

function customEnvelopes(){
	$('#custom-envelopes a').click(function(){
		return false;
	});
	$('#custom-envelopes img').mouseover(function(){
		var src = $(this).attr('src').split('-')[0];
		$(this).attr('src', src + '.jpg');
		$('#preview img').attr('src',src + '-big.jpg');
	});
	
	$('#custom-envelopes img').mouseout(function(){
		var src = $(this).attr('src').split('.')[0];
		$(this).attr('src', src + '-off.jpg');
		$('#preview img').attr('src',src + '-big.jpg');
	});
}

function sorting(){
	var sorting = $('#BodyContent__ctl0_region_SortByList');
	sorting.change(function(){
		var ind = $(this).val();
		var loc = window.location.toString(); 
		var ques = loc.indexOf( '?');
			var url;
			if (ques != -1){
				url = loc.substring(0, ques) + ind;
			} else {
				url = loc + ind;
			}
			window.location = url;			
	});
}

function emailFriend(){
	$('.email-friend').click(function(){
		GB_showCenter( 'Email a Friend', $(this).attr('href'),  600, 600);
		return false;
	});
}

function accordianMenu(){
	$('#sidemenu').accordion({ 
		header:  'dt',
    	event: 'mouseover',
    	fillSpace: true
	});	
}


function clearTextboxes(){
$('.text').click(function(){			
	var str = $(this).attr('value');
	if(str == $(this).attr('title')){
		$(this).attr({value:  ' '});
		$(this).select();
	}
});
$('.text').blur(function(){			
	var str  = $(this).attr('value');
	if((str ==  ' ') || (str == undefined)){
		$(this).attr({value:$(this).attr('title')});
	}	
});	
}

function initBanners(){
	$('#controls ol').tabs({ fx: { opacity: 'toggle' },
						   show:function(ui){
						   		var selected = $( '#controls ol ').data('selected.tabs');
								if((selected + 1) >= $('#controls ol li').length){
									$('#control-next').fadeTo( 'fast ',0.33);
									$('#control-previous').fadeTo( 'fast ',1);
								}
								else if(selected == 0){
									$('#control-previous').fadeTo( 'fast ',0.33);
									$('#control-next').fadeTo( 'fast ',1);
								}
								else{
									$('#control-next').fadeTo( 'fast ',1);
									$('#control-previous').fadeTo( 'fast ',1);
								}
								
		
						   }
						   }).tabs('rotate', 5000);
	
	$('#control-next').click(function(){
		$( '#controls ol').tabs('rotate',null); 
		var selected = $('#controls ol').data('selected.tabs');
		$('#controls ol').tabs('select',selected + 1);
		$('#control-pause').addClass('none');
		$('#control-play').removeClass('none');
		return false;
	});
	
	$('#control-previous').click(function(){
		$('#controls ol').tabs('rotate',null); 
		var selected = $('#controls ol').data('selected.tabs');
		$('#controls ol').tabs('select',selected - 1);
		$('#control-pause').addClass('none');
		$('#control-play').removeClass('none');
		return false;
	});
	
	$('#control-pause').click(function(){
		$('#controls ol').tabs('rotate',null); 
		$(this).addClass('none');
		$('#control-play').removeClass('none');
		return false;
	});
	
	$('#controls ol  a').click(function(){
		$('#controls ol').tabs('rotate',null); 
		$('#control-pause').addClass('none');
		$('#control-play').removeClass('none');
		return false;
	});
	
	$('#control-play').click(function(){
		$('#controls ol').tabs('rotate',5000); 
		$(this).addClass('none');
		$('#control-pause').removeClass('none');
		return false;
	});	
}


function gallery(){
	$('#gallery img').mouseover(function(){ 
		$(this).addClass('current');								 
		//$('#gallery img:not(.current)').fadeTo('fast',0.5);	
		$('#gallery img:not(.current)').css('opacity',0.5);
		
		//$('#gallery img').css('margin-left',0);	
		//$(this).fadeTo('fast',1.0);		
	//	$(this).css('margin-top',2);
		//$(".block").animate({'margin-top': '2px'}, 'slow');
	});
	$('#gallery img').mouseout(function(){
		$('#gallery img').css('opacity',1);
		$(this).removeClass('current');
		//$('#gallery img').fadeTo('slow',1.0);				   
	});

}


function carousel(){
  $('#carousel').css('position','relative'); 	
  var total_margin;
  var pointer;
  init();
  function init(){
  	total_margin = 13;
  	$('#carousel li').each(function(i){
		$(this).css('position','absolute');
		$(this).css('left', (i * 230) + (total_margin * i));
	  });
	  reset();
  }
  
  function reset(){
  	total_margin = 13;
	pointer = 0;
  }

  $('#next').click(function(){
  	if(pointer < 3){
		$('#carousel li').each(function(i){
			var new_pos = parseInt($(this).css('left')) - (230 * 4) - (13 * 4);
			//$(this).css('left', new_pos);
			$(this).animate({'left': new_pos},'fast');
  		});
		pointer = pointer + 1;
		update(pointer)
	}
	else{
		reset();
	
		var first_pos = $('#carousel li:eq(0)').css('left');
		$('#carousel li').each(function(i){
			//var new_pos = parseInt(first_pos) - parseInt($(this).css('left'));
			
			//$(this).animate({'left': new_pos},'fast');
			 
			var new_pos = i * 230 + (total_margin * i);
			$(this).animate({'left': new_pos},'fast');
	  });
	  update(pointer);
	}
	return false;
  });
  $('#previous').click(function(){
  	if(pointer > 0){
		$('#carousel li').each(function(i){
			var new_pos = parseInt($(this).css('left')) + (230 * 4) + (13 * 4);
			$(this).animate({'left': new_pos},'fast');
  		});
		pointer = pointer - 1;
		update(pointer)
	}
	else{
		pointer = 3;
		
		$('#carousel li').each(function(i){
			var new_pos = parseInt($(this).css('left')) - (230 * 12) - (13 * 12);
			
			$(this).animate({'left': new_pos},'fast');
	  });
	  update(pointer);
	}
	return false;
  });
  
  function update(index){
  	index = index % 4;
	$('.location img').each(function(){
		$(this).attr('src','/webresources/images/scroller_off.gif');
	});
	switch(index){
		case 0:
			$('.location img:eq(0)').attr('src','/webresources/images/scroller_on.gif');
			break;
		case 1:
			$('.location img:eq(1)').attr('src','/webresources/images/scroller_on.gif');
			break;
		case 2:
			$('.location img:eq(2)').attr('src','/webresources/images/scroller_on.gif');
			break;
		case 3:
			$('.location img:eq(3)').attr('src','/webresources/images/scroller_on.gif');
			break;
	}
  }
}


function addMediumShadow(){
	var medium = $('#medium img').get(0);
	var h = medium.height;
	var w = medium.width;
	if((h==350) && (w==250)){
		$(medium).parent().addClass('mediumVertical2')
	}
	
	if((h==267) && (w==200)){
		$(medium).parent().addClass('mediumVertical1')
	}
	
	if((h==200) && (w==267)){
		$(medium).parent().addClass('mediumHorizontal1')
	}		
}

function calculator(){
	$('#pricing .total td:eq(0)').text(formatCurrency($('#total').val() * itemPrice));
	$('#pricing tr:eq(0) td:eq(2)').text(formatCurrency($('#total').val() * itemPrice));
	$('#total').change(function(){
		$(this).val() * itemPrice ;
		$('#pricing tr:eq(0) td:eq(2)').text(formatCurrency($('#total').val() * itemPrice));
		$('#pricing .total td:eq(0)').text(formatCurrency($(this).val() * itemPrice));
	});
}

function priceBreak(){
	if(priceBreaks.length > 0){
		$('#total').css('display','none');
		var breaks = new Array();
		
		for(var i=0; i<priceBreaks.length; i++){
			breaks[i] = new Array();
			breaks[i][0] = priceBreaks[i].split(',')[0];
			breaks[i][1] = priceBreaks[i].split(',')[1];	
		}
		createOptions(breaks);
	}
}

function createOptions(array){
	var select_tag = document.createElement('select');
	select_tag.id = 'total2';
	var options = $('.options').get(0);
	options.appendChild(select_tag);
	for(var i=0; i<array.length; i++){
		var option = document.createElement('option');
		option.value = array[i][0];
		option.title = array[i][1];
		option.innerHTML = array[i][0];
		select_tag.appendChild(option);
	}
	
	var highest = parseInt(array[array.length-1][0]);
	highest = highest + 25;
	while(highest<=400){
		var option = document.createElement('option');
		option.value = highest;
		option.title = array[array.length-1][1];
		option.innerHTML = highest
		select_tag.appendChild(option);
		highest = highest + 25;
	}
	
	var option = document.createElement('option');
	option.value = 500;
	option.title = array[array.length-1][1];
	option.innerHTML = 500
	select_tag.appendChild(option);
	
	var option = document.createElement('option');
	option.value = 500;
	option.title = array[array.length-1][1];
	option.innerHTML = "Call for 500+";
	option.disabled = true;
	select_tag.appendChild(option);
	
	$('.price').text(formatCurrency(array[0][1]));
	$('#pricing .total td:eq(0)').text(formatCurrency($('#total2').val() * array[0][1]));
	$('#pricing tr:eq(0) td:eq(2)').text(formatCurrency($('#total2').val() * array[0][1]));
	
	$(select_tag).change(function(){
		var dom_select = $(this).get(0);
		var itemPrice = dom_select.options[dom_select.selectedIndex].title;
		$('.price').text(formatCurrency(itemPrice));
		$('#pricing tr:eq(0) td:eq(2)').text(formatCurrency($(this).val() * itemPrice));
		$('#pricing .total td:eq(0)').text(formatCurrency($(this).val() * itemPrice));
	});
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
	
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}
  
var supersleight=function(){var root=false;var applyPositioning=true;var shim='http://www.finemoments.com/webresources/images/x.gif';var shim_pattern=/x\.gif$/i;var fnLoadPngs=function(){if(root){root=document.getElementById(root)}else{root=document}for(var i=root.all.length-1,obj=null;(obj=root.all[i]);i--){if(obj.currentStyle.backgroundImage.match(/\.png/i)!==null){bg_fnFixPng(obj)}if(obj.tagName=='IMG'&&obj.src.match(/\.png$/i)!==null){el_fnFixPng(obj)}if(applyPositioning&&(obj.tagName=='A'||obj.tagName=='INPUT')&&obj.style.position===''){obj.style.position='relative'}}};var bg_fnFixPng=function(obj){var mode='scale';var bg=obj.currentStyle.backgroundImage;var src=bg.substring(5,bg.length-2);if(obj.currentStyle.backgroundRepeat=='no-repeat'){mode='crop'}obj.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='"+mode+"')";obj.style.backgroundImage='url('+shim+')'};var el_fnFixPng=function(img){var src=img.src;img.style.width=img.width+"px";img.style.height=img.height+"px";img.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='scale')";img.src=shim};var addLoadEvent=function(func){var oldonload=window.onload;if(typeof window.onload!='function'){window.onload=func}else{window.onload=function(){if(oldonload){oldonload()}func()}}};return{init:function(){addLoadEvent(fnLoadPngs)},limitTo:function(el){root=el},run:function(){fnLoadPngs()}}}();if($.browser.msie){if($.browser.version==6.0||$.browser.version==5.5){supersleight.init()}}