// JavaScript Document
/*
 * Author: Athanasios Theodorou
 * Filename: script.js
 * Description: This file contains javascript function to handle
 * everything on the page.
 * Copyright &copy; 2011, Computerman
 */

$(document).ready(function() {	
	var url = 'http://' + window.location.host + '/';
	var speed = 1000;
	/* Display informational */
	$('.informational').css('display', 'none');
	$('.informational').html('<small><a href="'+url+'cm_panel.php" target="_self">Admin</a>&nbsp;|&nbsp;Copyright &copy; 2011, G&Z Furs | Designed and Developed by <a href="http://www.computerman.gr/" target="_blank" index="follow">Computerman.</a></small>');
	$('.informational').css('display', '');
	/* Dynamic Parent */
	var max_height = 605;
	var current_vp_height = $(window).height();
	var x = ((100 - (Math.round(100 * (max_height / current_vp_height)))) / 2);
	
	if($(window).height() < 615) {
		$('#parent').css('top', '0');
	} else if($(window).height() > 615) {
		$('#parent').css('top', x+'%');
	}
	
	$(window).resize(function() {
		var max_height = 605;
		if($(window).height() > 605) {
			var current_vp_height = $(window).height();
			var x = ((100 - (Math.round(100 * (max_height / current_vp_height)))) / 2);
		} else {
			var x = 0;
		}
		$('#parent').css('top', x+'%');
	});
	
	// Determining the height
	var count = $('#menu ul li ul li').size();
	var max_height = count * '30';
	
	$('#collection').click(function () {
		var ht = $('#menu li ul').css('height');
		(ht == '0px') ? toggler = max_height : toggler = '0';
		$('#menu li ul').animate({
			height: toggler	},
			menu_animation_speed, function() {
				// Nothing
		});
	});
	
	$('#collection').click(function(event) {
		event.stopPropagation();
	});
	var menu_animation_speed = 300;
	$(document).click(function() {
		var ht = $('#menu li ul').css('height');
		var reg_ht = $('#registration_form').css('height');
		if(ht != '0px') {
			$('#menu li ul').animate({
				height: '0'
				}, menu_animation_speed, function() {
					// Nothing
			});
		}
		if(reg_ht != '0px') {
			$('#registration_form').animate({
				height: '0'},
			menu_animation_speed, function() {
				// Nothing
			});
		}
		// Add more events below this comment
		$('#menu form').animate({
			opacity: 0},
			speed, function(){
				$('#menu form').css('visibility', 'hidden');
		});
		$('.tooltip').detach();
	});
	
	$('#menu ul li ul li, #menu ul li ul').click(function(event) {
		$('#menu li ul').animate({
				height: '0'
				}, menu_animation_speed, function() {
					// Nothing
			});
		event.stopPropagation();
	});
	/* End of Menu Animations */
	
	/* Controlls the click event of the home link */
	$('#home').bind('click', function() {
		if($('#content_container').css('margin-left') == '0px') {
			window.location.reload()
		}
		$('#content_container').animate({
			marginLeft: '0'
			}, speed, function() {
		});
		$('#background').animate({
			marginLeft: '0' },
			speed, function () {
		});
		$('#collection_controlls').animate({
				marginLeft: '3000'},
				speed, function() {
		});
		$('#col_2011_header').animate({
			marginLeft: '1150'},
			speed, function() {
		});
		inc_step = -1100;
	});
	
	/* Controlls teh click event of the Current Collection */
	$('#col_2011').bind('click', function() {
		$('#content_container').animate({
			marginLeft: '-1100'
			}, speed, function() {
		});
		$('#background').animate({
			marginLeft: '-100' },
			speed, function () {
		});
		$('#collection_controlls').animate({
			marginLeft: '735'},
			speed, function() {
		});
		$('#col_2011_header').animate({
			marginLeft: '50'},
			speed, function() {
		});
	});
	
	/* Controlls the click event of te contact link */
	$('#contact').bind('click', function() {
		$('#content_container').animate({
			marginLeft: '1050'}, speed, function() {
		});
		$('#col_2011_header').animate({
			marginLeft: '3000'},
			speed, function() {
		});
		$('#collection_controlls').animate({
				marginLeft: '3000'},
				speed, function() {
		});
		$('#background').animate({
			marginLeft: '100' },
			speed, function () {
		});
	});
	
	/* Controlls the Click event of the gallery controlls */
	var inc_step = -1100;
	var bg_inc = -100;
	$('#next').bind('click', function() {
		if(inc_step != -9500) {
			inc_step = inc_step - 1200;
			bg_inc = bg_inc - 100;
			$('#content_container').animate({
				marginLeft: inc_step
				}, speed, function() {
			});
			$('#background').animate({
				marginLeft: bg_inc },
				speed, function () {
			});
			if(inc_step == -9500) {
				$('#collection_controlls').animate({
					marginLeft: '365'},
					speed, function() {
				});
			}
		}
	});
	
	/* Header click event controlls */
	$('#col_2011_header').bind('click', function() {
		$('#content_container').animate({
			marginLeft: '-1100'
			}, speed, function() {
		});
		$('#background').animate({
			marginLeft: '-100' },
			speed, function () {
		});
		$('#collection_controlls').animate({
			marginLeft: '735'},
			speed, function() {
		});
		$('#col_2011_header').animate({
			marginLeft: '50'},
			speed, function() {
		});
		inc_step = -1100;
	});
	
	$('#previous').bind('click', function() {
		if(inc_step != -1100) {
			inc_step = inc_step + 1200;
			bg_inc = bg_inc + 100;
			$('#content_container').animate({
				marginLeft: inc_step
				}, speed, function() {
			});
			$('#background').animate({
				marginLeft: bg_inc },
				speed, function () {
			});
			if(inc_step == -8300) {
				$('#collection_controlls').animate({
					marginLeft: '735'},
					speed, function() {
				});
			}
		} else {
			$('#content_container').animate({
				marginLeft: '0'
				}, speed, function() {
			});
			$('#collection_controlls').animate({
				marginLeft: '3000'},
				speed, function() {
			});
			bg_inc = bg_inc + 100;
			$('#background').animate({
				marginLeft: bg_inc },
				speed, function () {
			});
			$('#col_2011_header').animate({
				marginLeft: '1150'},
				speed, function() {
			});
			inc_step = -1100;
			bg_inc = -100;
		}
	});
	
	$('#prev_collections').bind('click', function() {
		$.post(url + "index.php/home/ajax_token", {'ajax_token' : $.cookie('tokens')}, function(data) {
			if(!data) {
				$('#menu form').css('visibility', 'visible');
				$('#menu form').animate({
					opacity: 1},
					speed, function(){
						var tt = $('#login_button').attr("title");
						var elem_pos = $('#login_button').position();
						var left_pos = elem_pos.left + 2;
						var tt_html  = '<div class="tooltip" style="font-size:10px !important; color:white; width:120px; position:absolute; bottom:40px; left:' + left_pos + 'px; z-index:-400;">' + tt + '</div>';
						$('#tt').append(tt_html);
				});
			} else {
				window.location.replace(url + 'index.php/collections');
			}
		});
	});
	
	$('#menu form').click(function(event) {
		event.stopPropagation();
	});
	
	/* Maps Controlls */
	$('#map1').bind('click', function() {
		$('#allmaps').animate({
			marginLeft: '-440'},
			500, function() {
		});
	});
	
	$('#map2').bind('click', function() {
		$('#allmaps').animate({
			marginLeft: '0'},
			500, function() {
		});
	});
	
	$("a[rel='col_2011']").colorbox({returnFocus: false});
	
	$("img.reflect").reflect({'height' : '30', 'opacity' : '0.35'});
	
	$('#scrollbar1').tinyscrollbar({sizethumb: 60});
	$('#scrollbar2').tinyscrollbar({sizethumb: 60});
	
	/* Registration Form */
	$('#register_fake').bind('click', function() {
		(($('#registration_form').css('height') != '360px') ? f_height = '360' : f_height = '0')
		$('#registration_form').animate({
			height: f_height},
			menu_animation_speed, function() {
		});
	});
	
	$('#registration_form').click(function(event) {
		event.stopPropagation();
	});
	/* Cookies Test */
	$.cookie("example", "foo", { path: '/', expires: 7 });
	
	$('input').bind('focus', function(event) {
		if(event.target.name == 'fake_pass1') {
			$('input[name="'+event.target.name+'"]').css('display', 'none');
			$('input[name="pass1"]').css('display', 'block');
			$('input[name="pass1"]').focus();
		}
		if(event.target.name == 'fake_pass2') {
			$('input[name="'+event.target.name+'"]').css('display', 'none');
			$('input[name="pass2"]').css('display', 'block');
			$('input[name="pass2"]').focus();
		}
		if($('input[name="'+event.target.name+'"]').val() == event.target.title) {
			$('input[name="'+event.target.name+'"]').val('');
		}
	});
	
	$('input').bind('blur', function(event) {
		var blurttl = event.target.title;
		if($('input[name="'+event.target.name+'"]').val() == '') {
			$('input[name="'+event.target.name+'"]').val(blurttl);
		}		
	});
	
	/* AJAX Functionality */	
	$('#submit_contact').bind("click", function() {
		var error_free = {'border': 'none'};
		$('#contact_form input, #textarea').css(error_free);
		
		var error_st = {'border-width': '1px', 'border-style': 'dashed', 'border-color': 'red' };
		var first_name = $('#contact_form input[name="name"]').val();
		var last_name = $('#contact_form input[name="lastname"]').val();
		var phone = $('#contact_form input[name="phone"]').val();
		var email = $('#contact_form input[name="email"]').val();
		var comments = $('#textarea').val();
		if(comments != "Comment" && comments != "") {
			$.post(url + "index.php/home/comment", $("#contact_form").serialize(), function(data) {
				var data = $.parseJSON(data);
				if(data.errors) {
					$.each(data.errors, function(key, value) {
						$('#contact_form input[name="'+value+'"]').css(error_st);
					});
					alert(data.msg);
					return false;
				} else {
					alert(data.msg);
					window.location.reload();
					return false;
				}
			});			
			return false;
		} else {
			$('#textarea').css(error_st);
			alert('Please add a comment!');
			return false;
		}
	});

});
