$(function(){
    var username  = 'motorolleri_lv';
    var classes   = ['first','second','third','first','second','third','first','second','third','first','second','third'];
    var mName     = readCookie('m-name');
    var mMail     = readCookie('m-mail');
    var mURL      = readCookie('m-url');
    var vidWidth  = 459;
    var vidHeight = 283;
    var obj       = '<div class="youtube-vid"><object width="' + vidWidth + '" height="' + vidHeight + '">' +
        '<param name="movie" value="http://www.youtube.com/v/[vid]&amp;hl=en&amp;fs=1">' +
        '</param><param name="allowFullScreen" value="true"></param><param ' +
        'name="allowscriptaccess" value="always"></param>' +
        '<param name="wmode" value="transparent"></param><em' +
        'bed src="http://www.youtube.com/v/[vid]&amp;hl=en&amp;fs=1" ' +
        'type="application/x-shockwave-flash" wmode="transparent" allowscriptaccess="always" ' +
        'allowfullscreen="true" width="' + vidWidth + '" ' + 'height="' +
        vidHeight + '"></embed></object></div>';

    // twitter pēdējā ieraksta iegūšana un attēlošana
	$.getScript('http://twitter.com/statuses/user_timeline/' + username + '.json?callback=twitterCallback2&count=1');

    // tekstuālās reklāmas
    $('.text-ads').randomContent();

     // e-mail, name, url autofill
    if (mName != null) $('#id_author_name').val(mName);
    if (mMail != null) $('#id_author_email').val(mMail);
    if (mURL  != null) $('#id_author_url').val(mURL);

    // target="_blank" for XHTML strict and HTML strict
    $('a[rel*=external]').click(function(){
        window.open(this.href);
        return false;
    });

    $('#m-nav li:last').addClass('last');
    $('.breadcrumbs a:first').addClass('home');
    $('.breadcrumbs a:last').addClass('current');
    $('#blog h2:first').addClass('first');
    $('#blog .img-gal img:last').addClass('last');

    // meklēšanas lauka notīrīšana un formas apstiprināšana
    $('#srch input').search();
    $('#srch #submit-search').click(function(){
        $('#srch #search-form').submit();
        return false;
    });

    // youtube video replacement
    $('#blog').find('p:contains("youtube.com/watch")').each(function(){
        var that = $(this);
        var vid = that.html().match(/(?:v=)([\w\-]+)/g);
        if (vid.length) {
            $.each(vid, function(i){
                that.replaceWith( obj.replace(/\[vid\]/g, this.replace('v=','')) );
            });
        }
    });
	
	// motorolleru/motociklu info bloks
    $('#moto-inf .more-info a:first').click(function(){
        $('#moto-inf .more-info .more-info-op').slideToggle('slow');
        return false;
    });

    // motorolleru info
    $('#m-list li').hoverIntent(slideFadeToggle,slideFadeToggle);

    // motorolleru slīdrādes klases
    classes.sort(randOrd);
    $('#sldr-c ul li').each(function(i,val){
        $(this).addClass(classes[i]);
    });

    // motorolleru slīdrāde
    $('#sldr-c').jCarouselLite({
        btnNext: '#sldr-l',
        btnPrev: '#sldr-r',
        auto: 2000,
        speed: 500
    });

    // fancybox
	$('a[rel=gallery]').fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
		'titlePosition' : 'over',
		'titleFormat'   : function(title, currentArray, currentIndex, currentOpts) {
		    return '<span id="fancybox-title-over"><em>Bilde ' +  (currentIndex + 1) + ' / ' + currentArray.length + '</em>' + title + '</span>';
		}
	});

    // form validation
    $('#comment-form #cForm,#contact-form #cForm,.more-info-op #cForm').validate({
        submitHandler: function(form){
            var mNamei = $('#id_author_name').val();
            var mMaili = $('#id_author_email').val();
            var mURLi  = $('#id_author_url').val();
            createCookie('m-name',mNamei,356);
            createCookie('m-mail',mMaili,356);
            if(mURLi != undefined){
                createCookie('m-url',mURLi,356);
            }
            form.submit();
        },
        debug: true,
		errorElement: 'em',
		success: function(label){
			label.text('ok!').addClass('success');
		},
        rules:{
			id_author_name:'required',
			id_author_email:{
				required:true,
				email:true
			},
            id_author_url:{
                url:true
            },
            id_author_phone:{
                digits:true
            },
			id_body:{
				required:true,
				maxlength:2000
			}
		}
    });
});


var flashvars = {};
var params = {};
    params.wmode   = "transparent";
var attributes = {};
swfobject.embedSWF("/static/media/vespaspiets.swf", "vespa-spiets", "323", "70", "9.0.0", "/static/media/expressInstall.swf", flashvars, params, attributes);
