jQuery(document).ready(function ($) {
	var addThis = '<div class="addthis_toolbox addthis_default_style "><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>';
	
	if($('#comments_div').length) {
		$(addThis).insertBefore('#comments_div');
	} else {
		$(addThis).appendTo('#content');
	}
	
	var tweeturl = "http://api.twitter.com/status/"
				+"user_timeline/skidmarques.json?count=7"
				+"&callback=?";
	$.getJSON(tweeturl, function(data){
		$.each(data, function(i, item) {
			var txt = item.text
					.replace(/(https?:\/\/[-a-z0-9._~:\/?#@!$&amp;\'()*+,;=%]+)/ig,'<a target="_blank" href="$1">$1</a>')
					.replace(/@+([_A-Za-z0-9-]+)/ig, '<a target="_blank" href="http://twitter.com/$1">@$1</a>')
					.replace(/#+([_A-Za-z0-9-]+)/ig, '<a target="_blank" href="http://search.twitter.com/search?q=$1">#$1</a>');
			$('<li></li>').hide().addClass(i%2 ? 'even' : 'odd').html(txt).appendTo('.twitter').animate({opacity:'show'}, 1000);
		});
	});
	
	$('#twitter_icon').toggle(function(){
		var pos = $(this).offset();
		$('#twitter').css({top:pos.top, left:pos.left}).show();
		return false;	
	},
	function(){
		$('#twitter').hide();
		return false;
	});
	
    $('<link />', {
        'rel': "stylesheet",
        'type': "text/css",
        'media': "all",
        'href': "http://www.skidmarquesltd.com/wp-content/themes/skidmarques/js/colorbox.css"
    }).appendTo('head');
    $.getScript("http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js", function () {
        $.ajaxSetup({
            cache: true
        });
        var ajax_load = "<img src='js/images/loadinfo.gif' alt='loading...' />";
        $(".hide_if_javascript_enabled").css({
            display: "none"
        });
        $(".zero_width, .tab_selector li").css({
            width: 0
        });
        $("#main_menu, a.pannel, .tab_selector li a, #tabs, #show_blog_posts .post").hide();

        $("#bar").delay(1000).animate({
            width: 900
        }, 300);
        $("#tabs").tabs({
            fx: {
                opacity: "toggle"
            },
            show: function (event, ui) {

                var relativeY = $('.ui-state-active').position();
                $(".alloy_wheels .tab_selector, .tyres .tab_selector").stop().animate({
                    scrollTop: relativeY.top
                }, 1000, 'linear')

            }
        }).tabs("rotate", 5000, false);
        $(".ui-tabs-panel").css({
            height: 360
        });
        $(".alloy_wheels .ui-tabs-panel, .tyres .ui-tabs-panel").css({
            height: 500
        });

        $(".alloy_wheels .tab_selector, .tyres .tab_selector").css({
            height: 550,
            overflow: 'hidden'
        })
        $(".alloy_wheels .tab_selector a, .tyres .tab_selector a").mouseenter(function () {
            var relativeY = $(this).position();
            $(".alloy_wheels .tab_selector, .tyres .tab_selector").stop().animate({
                scrollTop: relativeY.top * 2
            }, 3000, 'linear')
        });
		
		if (window.localStorage){
	        var menuState = window.localStorage.getItem('menuState')
	        if (menuState == 'closed') {
	            $('.pagenav ul:first').hide();
	        }
        } else {
        	var menuState = 'open';
        }

        $('.pagenav h2').click(function () {
            
            if (menuState == 'closed') {
                $('.pagenav ul:first').show({
                    effect: "blind",
                    easing: 'easeOutBounce',
                    speed: 1000
                });
                if (window.localStorage){
                window.localStorage.setItem('menuState', 'open');
                }
            } else {
                $('.pagenav ul:first').hide({
                    effect: "blind",
                    easing: 'easeOutBounce',
                    speed: 1000
                });
                if (window.localStorage){
                window.localStorage.setItem('menuState', 'closed');
                }
            }
        });

        $('.alloy_wheels a.main_image, .tyres a.main_image').append('<span class="click_to_view" \>').hover(function () {
            $('span.click_to_view', this).fadeTo(200, 1);
        }, function () {
            $('span.click_to_view', this).fadeTo(200, 0);
        })

        $("#show_blog_posts_button").click(function () {
            $("#show_blog_posts .post").show({
                effect: "slide",
                direction: "up",
                easing: 'easeOutBounce',
                speed: 1000
            });
        });

        function showElements() {
            $("#bar").animate({
                width: 900
            }, 500);
            $("#tabs").show({
                effect: "slide",
                direction: "up",
                easing: 'easeOutBounce',
                speed: 1000
            });
            $("#main_menu").show({
                effect: "slide",
                direction: "left",
                easing: 'easeOutBounce',
                speed: 1000
            });
            $("a.pannel").delay(300).show({
                effect: "slide",
                direction: "left",
                easing: 'easeOutBounce',
                speed: 1000
            });
            $('#blanket').remove();

            var sai = 0,
                timer = 200;

            function showAll() {
                $(".tab_selector li a:eq(" + sai + ")").show({
                    effect: "slide",
                    direction: "right",
                    easing: 'easeOutBounce',
                    speed: 1000
                });
                sai++;
            }
            var showAll_timer = setInterval(showAll, timer);
        }

        if ($('#blanket').length) {
            $('#blanket').delay(4000).animate({
                opacity: 0
            }, 2000, function () {
                showElements();
            })
        } else {
            showElements();
        }

        // load with ajax from href
        $(".ajax").click(function () {
            var loadUrl = this.href;
            var loadPosition = $(this).next(".loaded_content");
            $(loadPosition).html(ajax_load).load(loadUrl + " #content");
            return false
        });

        //add class


        function addClass() {
            $('tr:odd').not('thead tr, .tablesorter tr').addClass('odd');
        }
        addClass();
        // autoclear inputs
        $(".autoclear").click(function () {
            if ($(this).val() == $(this).attr('defaultValue')) $(this).val("");
        }).blur(function () {
            if ($(this).val() == "") $(this).val($(this).attr('defaultValue'));
        });

        // add 'to top' link
        var bodyheight = $('body').height();
        var windowheight = $(window).height();
        if (bodyheight > windowheight) {
            $(".footer small").append(' | <a id="to_top" class="btn">go to top</a>')
        }
        $("#to_top").click(function () {
            $('html, body').animate({
                scrollTop: 0
            }, 'slow');
        });

        $.getScript("/wp-content/themes/skidmarques/js/jquery.colorbox-min.js", function () {
            $('.colorbox_iframe').colorbox({
                iframe: true,
                width: '80%',
                height: '80%'
            })
            $('.img_colorbox').colorbox({
                height: '80%'
            })
            $('.cuff_link').colorbox({
                height: '80%',
                rel: 'gallery'
            })
            $('.gallery-icon a').colorbox({
                height: '80%',
                rel: $('.gallery-icon a')
            })
            var this_src = '';
            $('div.flickr-thumb a').colorbox({
                rel: $('div.flickr-thumb a'),
                href: function () {
                    this_src = $('img', this).attr('src');
                    this_src = this_src.replace('_s', '');
                    return this_src;
                }
            });
        });

        $('#email_signup').submit(function () {
            // update user interface
            $('#response').html('Adding email address...');

            // Prepare query string and send AJAX request
            $.ajax({
                url: '/wp-content/themes/skidmarques/mailchimpAPI/store-address.php',
                data: 'ajax=true&email=' + escape($('#email').val()),
                success: function (msg) {
                    $('#response').html(msg);
                }
            });

            return false;
        });

        $("#ajax-contact-form").submit(function () {
            var str = $(this).serialize();
            $.ajax({
                type: "POST",
                url: "/wp-content/themes/skidmarques/mailer/contact.php",
                data: str,
                success: function (msg) {

                    $("#note").ajaxComplete(function (event, request, settings) {
                        if (msg == 'OK') // Message Sent? Show the 'Thank You' message and hide the form
                        {
                            result = '<div class="notification_ok">Your message has been sent. Thank you!</div>';
                            $("#fields").hide();
                        } else {
                            result = msg;
                        }
                        $(this).html(result).addClass('message');
                    });
                }
            });
            return false;
        });

        $('.accordion li p:first-child').click(function () {
            $(this).nextAll().toggle('slow');
            return false;
        }).css({
            display: 'block',
            background: '#f2f2f2',
            padding: 2,
            cursor: 'pointer'
        }).nextAll().hide();



    });
});
