/*
 * @Author - Mustafa Quilon(http://mustafaquilon.com) for IMAGE Infotainment Ltd. (http://imageil.com)
 *
 * @Project - Samyo / http://samyo.org
 * Copyright (c) 2010. All Rights Reserved.
 *
 */


/* this prevents dom flickering, needs to be outside of dom.ready event: */
document.documentElement.className += 'js_active';
/*end dom flickering =) */

jQuery.noConflict();

/*jCarousel Lite - http://www.gmarwaha.com/jquery/jcarousellite/ */
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(6($){$.1g.1w=6(o){o=$.1f({r:n,x:n,N:n,17:q,J:n,L:1a,16:n,y:q,u:12,H:3,B:0,k:1,K:n,I:n},o||{});8 G.R(6(){p b=q,A=o.y?"15":"w",P=o.y?"t":"s";p c=$(G),9=$("9",c),E=$("10",9),W=E.Y(),v=o.H;7(o.u){9.1h(E.D(W-v-1+1).V()).1d(E.D(0,v).V());o.B+=v}p f=$("10",9),l=f.Y(),4=o.B;c.5("1c","H");f.5({U:"T",1b:o.y?"S":"w"});9.5({19:"0",18:"0",Q:"13","1v-1s-1r":"S","z-14":"1"});c.5({U:"T",Q:"13","z-14":"2",w:"1q"});p g=o.y?t(f):s(f);p h=g*l;p j=g*v;f.5({s:f.s(),t:f.t()});9.5(P,h+"C").5(A,-(4*g));c.5(P,j+"C");7(o.r)$(o.r).O(6(){8 m(4-o.k)});7(o.x)$(o.x).O(6(){8 m(4+o.k)});7(o.N)$.R(o.N,6(i,a){$(a).O(6(){8 m(o.u?o.H+i:i)})});7(o.17&&c.11)c.11(6(e,d){8 d>0?m(4-o.k):m(4+o.k)});7(o.J)1p(6(){m(4+o.k)},o.J+o.L);6 M(){8 f.D(4).D(0,v)};6 m(a){7(!b){7(o.K)o.K.Z(G,M());7(o.u){7(a<=o.B-v-1){9.5(A,-((l-(v*2))*g)+"C");4=a==o.B-v-1?l-(v*2)-1:l-(v*2)-o.k}F 7(a>=l-v+1){9.5(A,-((v)*g)+"C");4=a==l-v+1?v+1:v+o.k}F 4=a}F{7(a<0||a>l-v)8;F 4=a}b=12;9.1o(A=="w"?{w:-(4*g)}:{15:-(4*g)},o.L,o.16,6(){7(o.I)o.I.Z(G,M());b=q});7(!o.u){$(o.r+","+o.x).1n("X");$((4-o.k<0&&o.r)||(4+o.k>l-v&&o.x)||[]).1m("X")}}8 q}})};6 5(a,b){8 1l($.5(a[0],b))||0};6 s(a){8 a[0].1k+5(a,\'1j\')+5(a,\'1i\')};6 t(a){8 a[0].1t+5(a,\'1u\')+5(a,\'1e\')}})(1x);',62,96,'||||curr|css|function|if|return|ul|||||||||||scroll|itemLength|go|null||var|false|btnPrev|width|height|circular||left|btnNext|vertical||animCss|start|px|slice|tLi|else|this|visible|afterEnd|auto|beforeStart|speed|vis|btnGo|click|sizeCss|position|each|none|hidden|overflow|clone|tl|disabled|size|call|li|mousewheel|true|relative|index|top|easing|mouseWheel|padding|margin|200|float|visibility|append|marginBottom|extend|fn|prepend|marginRight|marginLeft|offsetWidth|parseInt|addClass|removeClass|animate|setInterval|0px|type|style|offsetHeight|marginTop|list|jCarouselLite|jQuery'.split('|'),0,{}))

jQuery.extend( jQuery.easing,
{
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	}
});

// tipsy
(function($) {
    $.fn.tipsy = function(options) {

        options = $.extend({}, $.fn.tipsy.defaults, options);
        
        return this.each(function() {
            
            var opts = $.fn.tipsy.elementOptions(this, options);
            
            $(this).hover(function() {

                $.data(this, 'cancel.tipsy', true);

                var tip = $.data(this, 'active.tipsy');
                if (!tip) {
                    tip = $('<div class="tipsy"><div class="tipsy-inner"/></div>');
                    tip.css({position: 'absolute', zIndex: 100000});
                    $.data(this, 'active.tipsy', tip);
                }

                if ($(this).attr('title') || typeof($(this).attr('original-title')) != 'string') {
                    $(this).attr('original-title', $(this).attr('title') || '').removeAttr('title');
                }

                var title;
                if (typeof opts.title == 'string') {
                    title = $(this).attr(opts.title == 'title' ? 'original-title' : opts.title);
                } else if (typeof opts.title == 'function') {
                    title = opts.title.call(this);
                }

                tip.find('.tipsy-inner')[opts.html ? 'html' : 'text'](title || opts.fallback);

                var pos = $.extend({}, $(this).offset(), {width: this.offsetWidth, height: this.offsetHeight});
                tip.get(0).className = 'tipsy'; // reset classname in case of dynamic gravity
                tip.remove().css({top: 0, left: 0, visibility: 'hidden', display: 'block'}).appendTo(document.body);
                var actualWidth = tip[0].offsetWidth, actualHeight = tip[0].offsetHeight;
                var gravity = (typeof opts.gravity == 'function') ? opts.gravity.call(this) : opts.gravity;

                switch (gravity.charAt(0)) {
                    case 'n':
                        tip.css({top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}).addClass('tipsy-north');
                        break;
                    case 's':
                        tip.css({top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2}).addClass('tipsy-south');
                        break;
                    case 'e':
                        tip.css({top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth}).addClass('tipsy-east');
                        break;
                    case 'w':
                        tip.css({top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width}).addClass('tipsy-west');
                        break;
                }

                if (opts.fade) {
                    tip.css({opacity: 0, display: 'block', visibility: 'visible'}).animate({opacity: 0.8});
                } else {
                    tip.css({visibility: 'visible'});
                }

            }, function() {
                $.data(this, 'cancel.tipsy', false);
                var self = this;
                setTimeout(function() {
                    if ($.data(this, 'cancel.tipsy')) return;
                    var tip = $.data(self, 'active.tipsy');
                    if (opts.fade) {
                        tip.stop().fadeOut(function() { $(this).remove(); });
                    } else {
                        tip.remove();
                    }
                }, 100);

            });
            
        });
        
    };
    
    // Overwrite this method to provide options on a per-element basis.
    // For example, you could store the gravity in a 'tipsy-gravity' attribute:
    // return $.extend({}, options, {gravity: $(ele).attr('tipsy-gravity') || 'n' });
    // (remember - do not modify 'options' in place!)
    $.fn.tipsy.elementOptions = function(ele, options) {
        return $.metadata ? $.extend({}, options, $(ele).metadata()) : options;
    };
    
    $.fn.tipsy.defaults = {
        fade: false,
        fallback: '',
        gravity: 'n',
        html: false,
        title: 'title'
    };
    
    $.fn.tipsy.autoNS = function() {
        return $(this).offset().top > ($(document).scrollTop() + $(window).height() / 2) ? 's' : 'n';
    };
    
    $.fn.tipsy.autoWE = function() {
        return $(this).offset().left > ($(document).scrollLeft() + $(window).width() / 2) ? 'e' : 'w';
    };
    
})(jQuery);


// navMain Animation Function
function navMain()
{
	jQuery("#navMain a").removeAttr('title');
	jQuery(" #navMain ul ").css({display: "none"}); // Opera Fix
	
	jQuery("#navMain li").each(function()
	{	
		
		var $sublist = jQuery(this).find('ul:first');
		
		jQuery(this).hover(function()
		{	
			$sublist.stop().css({overflow:"hidden", height:"auto", display:"none"}).slideDown(400, function()
			{
				jQuery(this).css({overflow:"visible", height:"auto"});
			});	
		},
		function()
		{	
			$sublist.stop().slideUp(400, function()
			{	
				jQuery(this).css({overflow:"hidden", display:"none"});
			});
		});	
	});
}

// Form Validation
function mForm(){
	var my_error;
	jQuery(".contactForm #send").bind("click", function(){
											 
	my_error = false;
	jQuery(".contactForm #name, .contactForm #message, .contactForm #emailContactPanel ").each(function(i){
				
				
				var value = jQuery(this).attr("value");
				var check_for = jQuery(this).attr("id");
				var surrounding_element = jQuery(this).parent();
				if(check_for == "emailContactPanel"){
					if(!value.match(/^\w[\w|\.|\-]+@\w[\w|\.|\-]+\.[a-zA-Z]{2,4}$/)){
						
						surrounding_element.attr("class","").addClass("error");
						
						my_error = true;
						}else{
						surrounding_element.attr("class","").addClass("valid");	
						}
					}
				
				if(check_for == "name" || check_for == "message"){
					if(value == ""){
						
						surrounding_element.attr("class","").addClass("error");
						
						my_error = true;
						}else{
						surrounding_element.attr("class","").addClass("valid");	
						}
					}
						   if(jQuery(".contactForm #name, .contactForm #message, .contactForm").length  == i+1){
								if(my_error == false){
									jQuery(".contactForm").slideUp(400);
									
									var $datastring = "ajax=true";
									jQuery(".contactForm input, .contactForm textarea").each(function(i)
									{
										var $name = jQuery(this).attr('name');	
										var $value = jQuery(this).attr('value');
										$datastring = $datastring + "&" + $name + "=" + $value;
									});
									
									jQuery(".contactForm #send").fadeOut(100);
									
									jQuery.ajax({
									   type: "POST",
									   url: "http://samyo.useconnect.co.uk/includes/send.php",
									   data: $datastring,
									   success: function(response){
									   jQuery(".contactForm").before("<div class='ajaxresponse' style='display: none;'></div>");
									   jQuery(".ajaxresponse").html(response).slideDown(400); 
									   jQuery(".contactForm #send").fadeIn(400);
									   jQuery(".contactForm #name, .contactForm #message, .contactForm #emailContactPanel").val("");
										   }
										});
									} 
							}
					});
			return false;
	});
}

/* Inititalize */

jQuery(document).ready(function() {
	
	//Settings to open links in a new window. Do not use target="blank" in the mark-up. Instead just give a class of external
	jQuery("a.external").click(function() {
		return !window.open(jQuery(this).attr("href"));
	});
	
	// Hide when javascript is enabled.
	jQuery(".hide").css("display", "none");
	
	// Settings for Contact Panel Slide Down
	jQuery("#contactLink").click(function(){
        jQuery("#contactPanel").slideToggle("slow");
        jQuery(this).parent().toggleClass("active"); return false;
    });
	
	// Settings for Guestbook Form Slide Down
	jQuery("#guestFormLink").click(function(){
        jQuery("#guestForm").slideToggle("slow");
        jQuery(this).parent().toggleClass("active"); return false;
    });
	
	// Settings for Tabs
	jQuery(".tabs").tabs({ fx: { opacity: 'toggle' } });
	
	// Settings for Accordion
	jQuery(".accordion").accordion({
			autoHeight: false,
			navigation: true
		});


	// Settings for the Top Link
	jQuery('.top a').click(function(){
     jQuery('html, body').animate({scrollTop: '0px'}, 300);
     return false;
});
	
	// Slideshow settings
	jQuery("#slideText").jCarouselLite({
		speed: 700,
		easing: "easeOutExpo",
		visible: 1,
		start: 0,
		auto: 12000,
        btnNext: ".next",
        btnPrev: ".previous"
    });
	jQuery("#slideImage").jCarouselLite({
		vertical: true,
		speed: 700,
		easing: "easeOutExpo",
		visible: 1,
		start: 0,
		auto: 12000,
        btnNext: ".next",
        btnPrev: ".previous"
    });
	jQuery("#affiliates").jCarouselLite({
		speed: 700,
		visible: 1,
		auto: 5000,
		easing: "easeOutExpo",
        btnNext: ".nextAffiliates",
        btnPrev: ".previousAffiliates"
    });
	jQuery("#partners").jCarouselLite({
		speed: 700,
		visible: 1,
		auto: 5000,
		easing: "easeOutExpo",
        btnNext: ".nextPartners",
        btnPrev: ".previousPartners"
    });
	
	// Pretty Photo(lightboxes) settings
	jQuery("a[rel^='modal']").prettyPhoto({
			showTitle: false,
			theme: 'facebook' /* light_rounded / dark_rounded / light_square / dark_square / facebook */
		});
	
	//image opacity animation
	jQuery('a.animate img').animate({ 'opacity' : 0.75 });	
	jQuery('a.animate img').hover(function() {
			jQuery(this).stop().animate({ 'opacity' : 1 });
		}, function() {
			jQuery(this).stop().animate({ 'opacity' : 0.75 });
		});
	
	//QuickLinks Section
	jQuery('#footer .quicklinks li').hover(function () {
		jQuery(this).stop().animate({
		'marginLeft' : 3
		}, 250);
	}, function () {
		jQuery(this).stop().animate({
		'marginLeft' : 0
		}, 250);
	});
	
	//Newsletter Section
	jQuery('a.animate img, a#newsFeed').animate({ 'top' : 96, 'opacity' : 0.75 });
	jQuery('a#newsFeed').hover(function () {
		jQuery(this).stop().animate({
		'top' : 92,
		'opacity' : 0.9
		}, 250);
	}, function () {
		jQuery(this).stop().animate({
		'top' : 96,
		"opacity" : 0.75
		}, 250);
	});
	
	// Tooltips Settings
	jQuery('.tooltipNorth').tipsy({gravity: 's', fade: true});
	jQuery('.tooltipSouth').tipsy({gravity: 'n', fade: true});
	jQuery('.tooltipEast').tipsy({gravity: 'w', fade: true});
	jQuery('.tooltipWest').tipsy({gravity: 'e', fade: true});
		
	navMain(); // navMain animation
	
	mForm(); // contact form
	
});

