$(document).ready(function(){
	
	
	var browser=navigator.appName;
	var b_version=navigator.appVersion;
	var version=parseFloat(b_version);
	
	if(browser.indexOf('Microsoft') == -1){
		$('div.scrollable').jScrollPane({showArrows:false, dragMinHeight:15, dragMaxHeight:15, scrollBarWidth:5});
	}
	//http://www.bevelspecs.com/blog/wp-content/flash/tradegothic.swf
	$("h1").sifr({font:'/assets/tradegothic', color: "#eae827"});
	$('h3').sifr({font:'/assets/tradegothic', color: "#eae827"});
	$('h2').sifr({font:'/assets/tradegothic', color: "#eae827"});

	showFrameInfo();
	
	$('#swatchbox a').live('click', function(el){
		id = parseInt($(this).attr('id'));
		$('#frame div').attr('style','background:white url(/assets/loading.gif) center no-repeat;');	
		$.getJSON('/frames/colors/'+id, function(data){

			$('#frame div').attr('style','background:white url(/images/frames/mid/'+id+'/) center no-repeat;');	
			$('#frame_color').html(data.code + ' | ' + data.name);
		});
		
	});
	
	$('#thumbs img').css('opacity','0.4');
	$('#thumbs img:first').css('opacity','1').addClass('selected');
	$('#thumbs img').live('click', loadFrame);
	$('#thumbs img').live('mouseover', hover);
	$('#thumbs img').live('mouseout', hoverOff);
	
	selectedFrame = $('#thumbs img:first').attr('id');
	
	$('fieldset.gallery img').css('opacity','0.2');
	$('fieldset.gallery img:first').css('opacity','1').addClass('selected');
	$('fieldset.gallery img').live('click', loadGalleryImage);
	
	
	offset = $('#retail').offset();
	width  = $('#retail').width();
	
	right = (offset.left + width) - 200;
	
	$('#regions').css('left', right+"px");
	$('#regions').css('visibility', 'visible');
	
	$('#zip').focus( function(){ if($(this).val() == "Zip Code") $(this).val(''); });
	$('#zip').blur( function(){ if($(this).val() == "") $(this).val('Zip Code'); });
	$("#zip").keypress(function(e){
		if($(this).val() != "Zip Code" && $(this).val() != "" && e.which == 13) zipSearch();
	});
	
	$("#kw").keypress(function(e){
		if(e.which == 13) search();
	});

});

var currentPage = 1;
var selectedFrame;
var enablePaging = true;

function loadFrame(){
		
	$('#thumbs img.selected').removeClass('selected').trigger('mouseout');
	$(this).css('opacity','1');
	$(this).addClass('selected');
	
	id = $(this).attr('id');
	selectedFrame = id;

	hideFrameInfo();
	$.getJSON('/frames/'+id, showFrame);
}

function hover(){
	if(!$(this).hasClass('selected')) $(this).css('opacity','0.75');
}

function hoverOff(){
	if(!$(this).hasClass('selected')) $(this).css('opacity','0.4');
}

function loadGalleryImage(){
	image = $(this).attr('rel');
	path  = $(this).attr('alt');
	$('fieldset.gallery img.selected').css('opacity','0.4').removeClass('selected');
	$(this).css('opacity','1').addClass('selected');
	
	$('#gallery-main div').attr('style','background:white url(/gallery/images/'+path+'/mid/'+image+') center no-repeat');
	$('#gallery-main div a').attr('href','/gallery/images/'+path+'/full/'+image);
}


function previousPage(){
	if(currentPage != 1 && enablePaging == true){
		currentPage--;

		var type 		= $("#thumbnails select[name='type']").val();
		var collection  = $("#thumbnails select[name='collection']").val();

		$.getJSON('/frames/page/'+currentPage+"/"+type+"/"+collection+"/", null, function(data){
			$('#thumbs div').empty();
			for(z = 0; z < data.length; z++){
				$('#thumbs div').append('<img src="/images/frames/thumb/'+data[z].image+'" id="'+data[z].id+'" />');
			}
		
			$('#thumbnails p em').text('Viewing page '+currentPage+' of '+totalPages);
			$('#thumbs img').css('opacity','0.4');
		
		});
	}
}

function nextPage(){
	if(currentPage != totalPages && enablePaging == true){
		currentPage++;
	
		var type 		= $("#thumbnails select[name='type']").val();
		var collection  = $("#thumbnails select[name='collection']").val();
	
		$.getJSON('/frames/page/'+currentPage+"/"+type+"/"+collection+"/" , null, function(data){
			$('#thumbs div').empty();
			for(z = 0; z < data.length; z++){
				$('#thumbs div').append('<img src="/images/frames/thumb/'+data[z].image+'" id="'+data[z].id+'" />');
			}
		
			$('#thumbnails p em').text('Viewing page '+currentPage+' of '+totalPages);
			$('#thumbs img').css('opacity','0.4');
			$("#thumbs img[id="+selectedFrame+"]").css('opacity','1').addClass('selected');
		});
	}
}

function showFrame(data){
	$('#frame_name').html(data.name);
	$('#frame_style').html('style: '+data.style);
	$('#frame_type').html('style: '+data.type);
	$('#frame_color').html(data.colors[0].name);
	$('#frame div').attr('style','background:white url(/images/frames/mid/'+data.colors[0].id+'/) center no-repeat;');	
	
	$('#swatchbox ul').empty();
	
	for(z = 0; z < data.colors.length; z++){
		$('#swatchbox ul').append('<li><a href="#" id="'+data.colors[z].id+'"><img src="/assets/swatches/'+data.colors[z].swatch+'" height="20" width="45"></a></li>');
	}
	
	showFrameInfo();
}

function hideFrameInfo(){
	$('#framebox ul.left').animate({ left:"0px" });
	$('#framebox ul.right').animate({ right:"0px" });
}

function showFrameInfo(){

	leftpos = $('#framebox ul.left').css('width');
	leftpos = parseInt(leftpos) - 2;
	$('#framebox ul.left').animate({ left:-leftpos+"px" });
	
	rightpos = $('#framebox ul.right').css('width');
	rightpos = parseInt(rightpos);
	$('#framebox ul.right').animate({ right:-rightpos+"px" });
}

function reloadThumbs(){
	
	var type 		= $("#thumbnails select[name='type']").val();
	var collection  = $("#thumbnails select[name='collection']").val();
	
	$.getJSON('/frames/sort/'+type + '/' +collection, null, function(data){
		$('#thumbs div').empty();
		
		if(parseInt(data.total) == 0){
			$('#thumbs div').html('<span>Your selection resulted in 0 frames.</span>');
			enablePaging = false;
		}else{
			for(z = 0; z < data.frames.length; z++){
				var frame = data.frames[z];
				$('#thumbs div').append('<img src="/images/frames/thumb/'+frame.image+'" id="'+frame.id+'" />');
			}
			
			enablePaging = true;
			currentPage = 1;
			totalPages  = Math.ceil(data.total / 14);
			$('#thumbnails p em').text('Viewing page '+currentPage+' of '+totalPages);
			$('#thumbs img').css('opacity','0.4');
			$("#thumbs img[id="+selectedFrame+"]").css('opacity','1').addClass('selected');
		}

	});
}

function search(){
	
	var kw = $('#kw').val();
	
	$.getJSON('/frames/search/'+kw + '/', null, function(data){
		$('#thumbs div').empty();

		if(parseInt(data.total) == 0){
			$('#thumbs div').html('<span>Your search resulted in 0 frames.</span>');
			enablePaging = false;
		
		}else{
			
			enablePaging = true;
			
			for(z = 0; z < data.frames.length; z++){
				var frame = data.frames[z];
				$('#thumbs div').append('<img src="/images/frames/thumb/'+frame.image+'" id="'+frame.id+'" />');
			}

			currentPage = 1;
			totalPages  = Math.ceil(data.total / 14);
			$('#thumbnails p em').text('Viewing page '+currentPage+' of '+totalPages);
			$('#thumbs img').css('opacity','0.4');
			$("#thumbs img[id="+selectedFrame+"]").css('opacity','1').addClass('selected');
		}
	});	
}

