﻿// JavaScript Document

// VARIABLES

var prev_section = 'home';
var section_old = '';
var sections = new Array('home','perfil','colecao','pronta-entrega','clipping','blog','contato');
var section_home = '/home';
var section = section_home;
var section_ref = 'home';
var sub_section = '';
var sub_section_param = '';
var sub_section_ref = '';
var viewingSubsection = false;

// Slideshow
var ss_total_photos = 8;
var ss_interval_time = 5000;
var ss_images = new Array();
for(var a = 0; a < ss_total_photos; a++)
{
	ss_images.push('images/jpg/background_home_look_' + (a + 1) + '.jpg');
}
var ss_images_loaded = false;
var ss_current_photo = 0;
var ss_playing = true;
var ss_opacity = 1;

var ss_images_object = new Array();

// Collection
var collection_total_events = 3;
var collection_step_total_events = 2;
var collection_loaded_events = 0;
var collection_step_events = 0;
var collection_xml = '';

// ADDs

Array.prototype.indexOf = function(obj, start)
{
     for (var i = (start || 0), j = this.length; i < j; i++)
	 {
         if (this[i] === obj) { return i; }
     }
     
	 return -1;
}


// BASE

function getBaseElement()
{
	var element = 'html';
	
	switch(true)
	{
		case (jQuery.browser.webkit == true || jQuery.browser.msie == true):
			element = 'body';
			
			break;
		case (jQuery.browser.version.slice(0,3) == '1.9' && jQuery.browser.mozilla):
			element = 'body';
			
			break;
	}
	
	return element;
}

var baseElement = getBaseElement();


// INIT

function init()
{
	startPreloadSlideshow()
				
	jQuery(document).bind
	(
		'resize', function()
		{
			var newHeight = jQuery(document).height() - 70;
			newHeight = (newHeight > 713) ? 713 : newHeight;
			jQuery('div#main.page').height(newHeight);
		}
	)
	
	jQuery(document).resize();
	
	jQuery(baseElement).bind
	(
		'collection_all_loaded',
		function()
		{
			switch(section)
			{
				case 'colecao':
					break;
			}
		}
	);
}


// AN (anchor navigation)

function setAddressChange()
{
	jQuery.address.change
	(
		function(_response)
		{
			old_section = section;
			
			section = _response.value == '/' ? section_home : _response.value;
			
			if((/colecao|pronta-entrega|clipping|contato/).test(section)) fixSlideshow(0);
			else if((/perfil/).test(section)) fixSlideshow(.3);
			else fixSlideshow(1);
			
			gotoSection(section);
			
			return false;
		}
	);
}


// CUSTOM

function startHighlight(_callback)
{
	jQuery('div#base.site div#main.page div.highlight').fadeIn('fast').delay(3000).fadeOut('fast');
}

function startPreloadSlideshow()
{
	jQuery.each
	(
		ss_images, function(_element)
		{
			var image = jQuery(new Image()).load
			(
				function()
				{
					ss_images_object.push(this);
					
					if (--ss_total_photos < 1) playSlideshow();
				}
			).attr('src', this);
		}
	);
}

function playSlideshow()
{
	var object = 'div#base.site div#main.page div#background';
	var node = 'img';
	
	if(scrollMax(object, node)) scrollRight(object, node, ss_interval_time, 'ss');
	else
	{
		jQuery(object).delay(ss_interval_time).animate
		(
			{ scrollLeft: 0 }, 100, function()
			{
				playSlideshow();
			}
		);
	}
}

function fixSlideshow(_opacity)
{
	ss_opacity = _opacity;
	
	jQuery('div#base.site div#main.page div#background div').animate
	(
		{
			'opacity': _opacity
		}, 0
	);
}

function showSection()
{
	jQuery('div#pageHolder').fadeIn('fast');
	
	if(section == 'colecao')
	{		
		switch(true)
		{
			case (sub_section == 'bolsas' || sub_section == 'cintos'):
				jQuery('div.colecao').addClass('viewing');
				
				nextCollection();
				
				break;
			default:
				jQuery('div.intro').fadeIn('fast');
				jQuery('div.colecao').removeClass('viewing');
			
				startCollection();
				
				//jQuery('div#base.site div#main.page div#pageHolder div.colecao').css({'margin-top': -jQuery('div#base.site div#main.page div#pageHolder div.colecao img.right').height() / 2})
				break;
		}
	}
	else if(section == 'clipping')
	{
		
	}
	else fixSlideshow(ss_opacity);
}

function verifyAllLoaded()
{
	if(collection_loaded_events >= collection_total_events) jQuery(baseElement).trigger('collection_all_loaded');
}	

function gotoSection(_section)
{
	prev_section = section_ref;
	
	sub_section = '';
	var link = _section.split('/');
	
	section = link[1];
	
	if(link.length > 2) if(link[2].length > 1) sub_section = link[2];
	if(link.length > 3) if(link[2].length > 1) sub_section_param = link[3];
	
	section_ref = section.replace(/-/g, '_');
	sub_section_ref = sub_section.replace(/-/g, '_');
	
	if(!(/home/).test(section)) jQuery('div.highlight').fadeOut('fast');

	if(old_section != section && old_section)
	{
		jQuery('div#pageHolder').fadeOut
		(
			'fast', function()
			{
				scrollReset('div.body');
				
				configSection();
			}
		);
	}
	else configSection();
}

function configSection()
{
	jQuery('div.' + prev_section.replace(/_/g, '-')).hide();
				
	switch(section)
	{
		case 'home':
				jQuery('div.highlight').fadeIn('fast', showSection);					
				
				startHighlight();
			break;
		case 'perfil':
				jQuery('div.' + section).fadeIn('fast', showSection);
			break;
		case 'colecao':
				jQuery('div.products').hide();
				
				loadCollection();
		
				jQuery('div.' + section).fadeIn('fast', showSection);
			break;
		case 'clipping':
				loadClipping();
		
				jQuery('div.' + section).fadeIn('fast', showSection);
			break;
		case 'pronta-entrega':
				jQuery('div.' + section).fadeIn('fast', showSection);
				
				getMap();
			break;
		case 'contato':
				jQuery('div#contactHolder div.form form').bind
				(
					'submit', function()
					{
						checkForm(jQuery(this).attr('id'));
						
						return false;
					}
				);
		
				jQuery('div.' + section).fadeIn('fast', showSection);
			break;
	}		
	
	jQuery('nav ul li[class = "selected"]').removeClass('selected');
	jQuery('nav ul li a[href ^= "#/' + section + '"]').parent().addClass('selected');
	jQuery('nav img.arrow').hide().removeClass(prev_section).addClass(section_ref).show();
}

function checkForm(_id)
{
	jQuery('form#' + _id + ' input[required="required"][value!=""], form#' + _id + ' textarea[required="required"][value!=""]').removeClass('empty');
											
	if(jQuery('form#' + _id + ' input[required="required"][value=""], form#' + _id + ' textarea[required="required"][value=""]').length)
	{
		jQuery('form#' + _id + ' input[required="required"][value=""], form#' + _id + ' textarea[required="required"][value=""]').addClass('empty');
		
		alert('Preencha os campos corretamente.');
	}
	else
	{
		var sign = _id.replace('frm_', '');
		
		if(new RegExp("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?").test(jQuery('form#' + _id + ' #tf_' + sign + '_email').val()))
		{
			jQuery.ajax
			(
				{
					url: 'ajax/contact.php?action=register&type=' + sign,
					type: 'POST',
					data: 'tf_name=' + jQuery('form#' + _id + ' #tf_' + sign + '_name').val() +  '&tf_phone=' + jQuery('form#' + _id + ' #tf_' + sign + '_phone').val() + '&tf_email=' + jQuery('form#' + _id + ' #tf_' + sign + '_email').val() +  '&tf_subject=' + jQuery('form#' + _id + ' #tf_' + sign + '_subject').val() +  '&tf_message=' + jQuery('#form#' + _id + ' #tf_' + sign + '_message').val(),
					success: function(_response)
					{
						alert('Sua mensagem foi enviada com sucesso');
					}
				}
			);
		}
		else
		{
			jQuery('form#frmContact input#tf_email').addClass('empty');
			
			alert('Preencha o campo de e-mail corretamente.');
		}
	}
}

function shareFacebook()
{
	
}


/* PRONTA-ENTREGA */

function getMap()
{
	var myLatlng = new google.maps.LatLng(-23.58742,-46.674128);
    var myOptions = 
	{
		zoom: 16,
		center: myLatlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
    }
	
	var image = 'images/png/logo_rosana_mattua_pin.png';    
	var map = new google.maps.Map(document.getElementById('mapHolder'), myOptions);
    
    var marker = new google.maps.Marker
	(
		{
			position: myLatlng, 
			map: map,
			icon: image, 
			title:"Rosana Mattua"
		}
	);
}


// Clipping

function loadClipping(_clipping)
{
	var collection = _clipping;
	
	jQuery.ajax
	(
		{
			type : "GET",
			url : "data/xml/clipping.xml",
			dataType: "xml",
			success : function(_xml)
			{
				clipping_xml = _xml;
				configClipping(sub_section);
			}
		}
	);
}

function configClipping(_clipping)
{
	var objects = jQuery(clipping_xml).find('photos photo');
			
	jQuery('div#base.site div#main.page div#pageHolder div.clipping div.photos div.photos_list div.body div').html('');

	jQuery.each
	(
		objects, function(_a, _object)
		{			
			var photo_id = (_a + 1);
			
			if(photo_id <= objects.length)
			{			
				var name = jQuery(clipping_xml).find('photos photo').eq(_a).attr('name');
				var image_1 = jQuery(clipping_xml).find('photos photo').eq(_a).find('images image').eq(0).attr('image');
				
				jQuery('div#base.site div#main.page div#pageHolder div.clipping div.photos div.photos_list div.body div').append("<a href=\"javascript: showPhoto(" + _a + ");\" title=\"" + name  +  "\"><img alt=\"" + name  +  "\" src=\"data/clipping/" + image_1 +  "\" /></a>");
			}
			else if(_a + 1 >= objects.length)
			{
				
			}
		}
	);
	
	if(sub_section_param) showPhoto(sub_section_param);
	else showPhoto(0);
}


// Coleção

function loadCollection(_collection)
{
	var collection = _collection;
	
	jQuery.ajax
	(
		{
			type : "GET",
			url : "data/xml/colecao.xml",
			dataType: "xml",
			success : function(_xml)
			{
				collection_xml = _xml;
				configCollection(sub_section);
			}
		}
	);
}

function configCollection(_collection)
{
	switch(true)
	{
		case (_collection == 'bolsas' || _collection == 'cintos'):
			var objects = jQuery(collection_xml).find('products#' + _collection + ' product');
			
			jQuery('div#base.site div#main.page div#pageHolder div.colecao div.products div.products_list div.body').scrollLeft(0);
			
			jQuery('div#base.site div#main.page div#pageHolder div.colecao div.products div.products_list div.body div').html('');
		
			jQuery.each
			(
				objects, function(_element)
				{	
					var product_id = (_element+ 1);
					var name = jQuery(collection_xml).find('products#' + _collection + ' product').eq(_element).attr('name');
					var image = jQuery(collection_xml).find('products#' + _collection + ' product').eq(_element).attr('image');
					
					if(image)
					{					
						jQuery('div#base.site div#main.page div#pageHolder div.colecao div.products div.products_list div.body div').append("<a href=\"javascript: showProduct(" + _element + ");\" title=\"" + name  +  "\"><img alt=\"" + name  +  "\" src=\"data/products/" + _collection +  "/" + image +  "\" /></a>");
					}
					
					if(_element + 1 >= objects.length)
					{
						jQuery('div#base.site div#main.page div#pageHolder div.colecao div.products div.products_list div.body').scrollLeft(0);
					}
				}
			);
			
			if(sub_section_param) showProduct(sub_section_param);
			else showProduct(0);
		
			break;
	}
}

function resetCollection()
{
	ss_playing = true;
}

function configNextCollection()
{
	if(collection_step_events >= collection_step_total_events)
	{
		jQuery('div.intro').fadeOut
		(
			'fast', function()
			{
				jQuery('div.products').fadeIn
				(
					'fast', function()
					{					
						if(sub_section_param && collection_loaded_events >= collection_total_events)
						{						
							showProduct(sub_section_param);
						}				
					}
				);
			}
		);
	}
}

function nextCollection()
{
	collection_step_events = 0;

	jQuery('div#base.site div#main.page div#pageHolder div.colecao div.button.left').animate
	(
		{
			'left': '-100%'
		}, 500, function()
		{
			collection_step_events++;
			
			configNextCollection();
		}
	);
	
	jQuery('div#base.site div#main.page div#pageHolder div.colecao div.button.right').animate
	(
		{
			'right': '-100%'
		}, 500, function()
		{
			collection_step_events++;
			
			configNextCollection();
		}
	);
}

function startCollection()
{
	jQuery('div#base.site div#main.page div#pageHolder div.colecao div.button.left map area').css({'position': 'relative', 'z-index': 1002});
	jQuery('div#base.site div#main.page div#pageHolder div.colecao div.button.right map area').css({'position': 'relative', 'z-index': 1003});
	
	jQuery('div#base.site div#main.page div#pageHolder div.colecao div.button.left').animate
	(
		{
			'left': '-35px'
		}, 500, function()
		{
			collection_step_events++;
				
			verifyAllLoaded();
		}
	);
	
	jQuery('div#base.site div#main.page div#pageHolder div.colecao div.button.right').animate
	(
		{
			'right': '-35px'
		}, 500, function()
		{
			collection_step_events++;
				
			verifyAllLoaded();
		}
	);
}


// PHOTO

function showPhoto(_photo)
{
	jQuery('div#photoHolder').fadeOut
	(
		'fast', function()
		{
			jQuery('div#photoHolder img').bind
			(
				'load', function()
				{
					jQuery('div#photoHolder').fadeIn('fast');
				}
			);	
			
			jQuery('div#photoHolder img.left').attr('src', 'data/clipping/' + jQuery(clipping_xml).find('photos photo').eq(_photo).find('images image').eq(0).attr('image'));
			if(jQuery(clipping_xml).find('photos photo').eq(_photo).find('images image').eq(1).attr('image'))
			{
				jQuery('div#photoHolder img.right').attr('src', 'data/clipping/' + jQuery(clipping_xml).find('photos photo').eq(_photo).find('images image').eq(1).attr('image'));
				jQuery('div#photoHolder img.right').show();
			}
			else jQuery('div#photoHolder img.right').hide();
		}
	);
}


// PRODUCT

function showProduct(_product)
{
	jQuery('div#productHolder').fadeOut
	(
		'fast', function()
		{
			jQuery('div#productHolder img').attr('src', 'data/products/' + sub_section +  '/' + jQuery(collection_xml).find('products#' + sub_section + ' product').eq(_product).attr('image'));			
			jQuery('div.box_left h3').html(jQuery(collection_xml).find('products#' + sub_section + ' product').eq(_product).attr('name'));			
			jQuery('div.box_left p span.ref').html(jQuery(collection_xml).find('products#' + sub_section + ' product').eq(_product).attr('ref'));			
			jQuery('div.box_left p.material').html(jQuery(collection_xml).find('products#' + sub_section + ' product').eq(_product).find('material').text());			
			jQuery('div.box_left p.size').html(jQuery(collection_xml).find('products#' + sub_section + ' product').eq(_product).attr('size'));
						
			var colors = jQuery(collection_xml).find('products#' + sub_section + ' product').eq(_product).find('colors color');			
			jQuery('div.box_left p.colors').html('');			
			jQuery.each
			(
				colors, function(_element, _object)
				{
					var box = "<a href=\"javascript: changeColor(" + _product + ", " + _element + ", '" + jQuery(_object).attr('image') + "');\"><span class=\"color\" style=\"background: " + jQuery(_object).attr('hex') +  ";\">&nbsp;</span></a>";
					
					jQuery('div.box_left p.colors').append(box);
				}
			);
			
			jQuery('div#productHolder').fadeIn('fast');
		}
	);
}

function changeColor(_product, _color, _image)
{
	jQuery('div#productHolder').fadeOut
	(
		'fast', function()
		{
			jQuery('div#productHolder img').bind
			(
				'load', function()
				{
					jQuery('div#productHolder').fadeIn('fast');
				}
			);
			
			jQuery('div#productHolder img').attr('src', 'data/products/' + sub_section + '/' + _image);
			
			var ref = jQuery(collection_xml).find('products#' + sub_section + ' product').eq(_product).find('colors color').eq(_color).attr('ref');
			if(ref) jQuery('div.box_left p span.ref').html(ref);
			
			var material = jQuery(collection_xml).find('products#' + sub_section + ' product').eq(_product).find('colors color').eq(_color).attr('material');
			if(material) jQuery('div.box_left p.material').html(material);
		}
	);
}


// SCROLL

function scrollReset(_object)
{
	jQuery(_object).animate
	(
		{ scrollLeft: 0 }, 100, function()
		{
		}
	);
}

function scrollMax(_object, _node)
{
	var speed_scroll = jQuery(_object).find('div.scroller').width() / jQuery(_object).find('div.scroller ' + _node).length;
	var max_scroll = jQuery(_object).find('div.scroller').width();
	var current_scroll = jQuery(_object).scrollLeft();
	var next_scroll = ((current_scroll + speed_scroll) <= max_scroll) ? current_scroll + speed_scroll : current_scroll;
	
	return ((current_scroll + speed_scroll) < max_scroll) ? true : false;
}

function scrollLeft(_object, _node, _delay, _callback)
{
	_delay = _delay ? _delay : 0;
	_callback = _callback ? _callback : '';
	
	var speed_scroll = jQuery(_object).find('div.scroller').width() / jQuery(_object).find('div.scroller ' + _node).length;
	var min_scroll = jQuery(_object).find('div.scroller').width();
	var current_scroll = jQuery(_object).scrollLeft();
	var next_scroll = ((current_scroll - speed_scroll) <= min_scroll) ? current_scroll - speed_scroll : current_scroll;
	
	jQuery(_object).delay(_delay).animate
	(
		{ scrollLeft: next_scroll }, 100, function()
		{
			 if (_callback == 'ss') { playSlideshow() };
		}
	);
}

function scrollRight(_object, _node, _delay, _callback)
{
	_delay = _delay ? _delay : 0;
	_callback = _callback ? _callback : '';
	
	var speed_scroll = jQuery(_object).find('div.scroller').width() / jQuery(_object).find('div.scroller ' + _node).length;
	var max_scroll = jQuery(_object).find('div.scroller').width();
	var current_scroll = jQuery(_object).scrollLeft();
	var next_scroll = ((current_scroll + speed_scroll) <= max_scroll) ? current_scroll + speed_scroll : current_scroll;
	
	jQuery(_object).delay(_delay).animate
	(
		{ scrollLeft: next_scroll }, 100, function()
		{
			 if (_callback == 'ss') { playSlideshow() };
		}
	);
}
