jQuery(document).ready(function(){
	/*initialize tabs*/
	jQuery(function() { 
		jQuery('.cosmo-tabs').tabs({ fxFade: true, fxSpeed: 'fast' }); 
	});

	/*toogle*/
	/*Case when by default the toggle is closed */
	jQuery(".open_title").toggle(function(){ 
			jQuery(this).next('div').slideDown();
			jQuery(this).find('a').removeClass('show');
			jQuery(this).find('a').addClass('hide');
			jQuery(this).find('.title_closed').hide();
			jQuery(this).find('.title_open').show();
		}, function () {
		
			jQuery(this).next('div').slideUp();
			jQuery(this).find('a').removeClass('hide');
			jQuery(this).find('a').addClass('show');		 
			jQuery(this).find('.title_open').hide();
			jQuery(this).find('.title_closed').show();
			
	});

	/*Case when by default the toggle is oppened */		
	jQuery(".close_title").toggle(function(){ 
			jQuery(this).next('div').slideUp();
			jQuery(this).find('a').removeClass('hide');
			jQuery(this).find('a').addClass('show');		 
			jQuery(this).find('.title_open').hide();
			jQuery(this).find('.title_closed').show();
		}, function () {
			jQuery(this).next('div').slideDown();
			jQuery(this).find('a').removeClass('show');
			jQuery(this).find('a').addClass('hide');
			jQuery(this).find('.title_closed').hide();
			jQuery(this).find('.title_open').show();
			
	});	
	
	/*Accordion*/
	jQuery('.cosmo-acc-container').hide();
	jQuery('.cosmo-acc-trigger:first').addClass('active').next().show();
	jQuery('.cosmo-acc-trigger').click(function(){
		if( jQuery(this).next().is(':hidden') ) {
			jQuery('.cosmo-acc-trigger').removeClass('active').next().slideUp();
			jQuery(this).toggleClass('active').next().slideDown();
		}
		return false;
	});

    
        //Slider
        jQuery('#slides').slides({
            "generateNextPrev":SL_BUTTONS ,
            "next": 'next_navigation',
            "prev": 'prev_navigation',
            "pagination" : false,
            "generatePagination": false,
            "slideSpeed":SL_SLIDESPEED ,
            "effect":SL_EFFECT,
            "randomize":SL_RANDOMIZE,
            "hoverPause":SL_PAUSE,
            "play":SL_PLAYSPEED,
            "autoHeight": true,
            /*"preload":true,
            "preloadImage":'/wp-content/themes/blogitty/images/loading.gif',*/

            "pause":2500,
            "animationStart":function(){
                jQuery('.caption').animate({
                    "bottom":-35
                },100);
            },
            "animationComplete": function(current){
                jQuery('.caption').animate({
                    "bottom":0
                },200);
                if (window.console && console.log) {
                    console.log(current);
                };
            }
        });
	
//slider navigation
	
	jQuery('.slider-container-pagination').mouseover(function() { 
		jQuery('.next_navigation').show();
		jQuery('.prev_navigation').show();
	}).mouseout(function(){
		jQuery('.next_navigation').hide();
		jQuery('.prev_navigation').hide();  
	});
	
	if( TESTIMONIALS ){
        //testimonials
        if (jQuery().slides) {
            jQuery("#testimonials_slider").slides({
                play: 0,
                //generateNextPrev: true,
                next: "next",
                prev: "prev",
                generatePagination: false,
                autoHeight: true
            });

        }
    }

	//twitter widget
	if (jQuery().slides) { 
		jQuery(".dynamic .cosmo_twitter").slides({
			play: 5000,
			effect: 'fade',
			generatePagination: false,
			autoHeight: true
		});	
		
	}

	//latest_projects carousell
    if( FP_LAYOUT ){
        jQuery(".latest_projects").jCarouselLite({
            btnNext: ".next_project",
            btnPrev: ".prev_project",
            speed: 500,
            easing: "easeinout",
            visible: 6
        });
    }
	
	//Mosaic fade
	jQuery('.circle').mosaic({
		opacity		:	0.7
	});
	
	jQuery('.gallery-icon').mosaic({
		opacity		:	0.7
	});
	
	jQuery('.fade').mosaic({
		animation	:	'slide'		//fade or slide
	});
	
	/*Initialize pretty galery
	 for more settings see http://www.no-margin-for-errors.com/projects/prettyphoto-jquery-lightbox-clone/documentation/
	 NOTE: this code must be placed after latest_projects carousell in order to have needed effect for latest project images
	 */
	jQuery("a[rel^='prettyPhoto']").prettyPhoto({
		autoplay_slideshow: false,
		theme: 'light_rounded'
	  
	});
	
	/* The following code is executed once the DOM is loaded */
	jQuery('.sponsorFlip').bind("click",function(){
		// jQuery(this) point to the clicked .sponsorFlip element (caching it in elem for speed):
		var elem = jQuery(this);
		// data('flipped') is a flag we set when we flip the element:
		if(elem.data('flipped'))
		{
			// If the element has already been flipped, use the revertFlip method
			// defined by the plug-in to revert to the default state automatically:
			elem.revertFlip();
			// Unsetting the flag:
			elem.data('flipped',false)
		}
		else
		{
			// Using the flip method defined by the plugin:
			elem.flip({
				direction:'lr',
				speed: 350,
				onBefore: function(){
					// Insert the contents of the .sponsorData div (hidden from view with display:none)
					// into the clicked .sponsorFlip div before the flipping animation starts:
					
					elem.html(elem.siblings('.sponsorData').html());
				}
			});
			// Setting the flag:
			elem.data('flipped',true);
		}
	});	
	
	

	//Back to top
	jQuery('.backtotop').click(function(){
		jQuery('html, body').animate({scrollTop:0}, 'slow');
	});
	
	//Superfish menu
	jQuery("ul.sf-menu").supersubs({
			minWidth:    12,
			maxWidth:    27,
			extraWidth:  1
		}).superfish({
			delay: 200,
			speed: 'fast'
		});
			
	//Cosmo-menu
	jQuery(".cosmo-menu").lavaLamp({ fx: "backout", speed: 1000});
	
	jQuery('.demo_btn').toggle(function() {
		jQuery(this).parent().find('div.demo_code').show();
		jQuery(this).html('- Code Snippet');
	  
	}, function() {
		jQuery(this).parent().find('div.demo_code').hide();
		jQuery(this).html('+ Code Snippet');
	  
	});


    /* front-page  */
    jQuery(".cosmo-swap").hoverIntent(makeTall,makeShort);

    function makeTall(){  jQuery(this).animate({"height":240},350);}
    function makeShort(){ jQuery(this).animate({"height":166},350);}

    jQuery('.fade').mosaic({speed : 350});
	
	jQuery('.flickr_badge_image').each(function(index){
		var x = index % 3;
		if(index !=1 && x == 2){
			
			jQuery(this).addClass('last');
		} 
	});
	
	jQuery('#select_zone').change(function() {
		var zone = jQuery('#select_zone').val(); 
		jQuery('.switcher-inputs').hide();
		jQuery('#'+ zone + '_inputs').show();
		if(zone == 'middle_bar'){
			jQuery('#patterns_inputs').hide();
		}
		else{
			jQuery('#patterns_inputs').show();	
		}
	});
	
	jQuery("#select_zone").val(jQuery("#select_zone option:first").val());
	
	
	jQuery('.show_colors').toggle(function(){ 
		jQuery(".style_switcher").animate({
		    left: "10px"
		    
		  }, 500 );
	}, function () {
		jQuery(".style_switcher").animate({
		    left: "-152px"
		    
		  }, 500 );
		
	});
});

	function setBgColor(rb_id,element){ 
		jQuery('#' + rb_id).trigger('click');
		changeBgColor(rb_id,element);
	}
	
	function setBgImage(rb_id){
		jQuery('#' + rb_id).trigger('click');
		changeBgImage(rb_id);
	}
	
    function send_contact_mail( action , form , container ){
        jQuery( document ).ready(function(){

            var name  = jQuery( form ).find("input[name=\"name\"]").val();
            var email = jQuery( form ).find("input[name=\"email\"]").val();
            var phone  = jQuery( form ).find("input[name=\"phone\"]").val();
            var mssg  = jQuery( form ).find("textarea[name=\"message\"]").val();

            
            jQuery.post( AJAXURL ,
                    {
                        "action" : action ,
                        "name" : name,
                        "email" : email,
                        "phone" : phone,
                        "message" : mssg,
                        "btn_send" : "btn_send"
                    } ,
                    function( data ){
                        var result = '';
                        var array  = data.split( '","' );
                        if( is_array( array ) ){
                            for(var i = 0; i < array.length; i++ ){
                                if( jQuery.trim(array[i]) == 'Error, fill all required fields ( email )' ){
                                    result = result + array[i] + '<br />';
                                    jQuery( form ).find("input[name=\"email\"]").addClass('send-error');
                                }

                                if( jQuery.trim(array[i]) == 'Error, fill all required fields ( name )' ){
                                    result = result + array[i] + '<br />';
                                    jQuery( form ).find("input[name=\"name\"]").addClass('send-error');
                                }

                                if( jQuery.trim(array[i]) == 'Error, fill all required fields ( message )' ){
                                    result = result + array[i] + '<br />';
                                    jQuery( form ).find("textarea[name=\"message\"]").addClass('send-error');
                                }
                            }
                            if( result.toString().length > 0 ){
                                jQuery( container ).html( result );
							}else{ 
                                jQuery( container ).html( data );
								
									jQuery('#name').val('');
									jQuery('#email').val('');
									jQuery('#comment').val('');
									jQuery('#contact_name').val('');
									jQuery('#contact_email').val('');
									jQuery('#contact_phone').val('');
									jQuery('#contact_message').val('');
							}
                        }else{
                            jQuery( container ).html( data ); 
						}
            });
        });
    }

    function is_array(obj) {
        if (obj.constructor.toString().indexOf("Array") == -1)
            return false;
        else
            return true;
    }

    jQuery( document ).ready(function(){
        jQuery('form#commentform').find('input').focus(function(){
            jQuery( this ).removeClass('send-error');
        });

        jQuery('form#commentform').find('textarea').focus(function(){
            jQuery( this ).removeClass('send-error');
        });
    });
    
    function doBoxed(obj){
    	if(jQuery(obj).attr('checked') ){
    		jQuery('body').addClass('larger');
    		jQuery.cookie("boxed",'yes', {expires: 365, path: '/'});
    		//setBoxedCookies('yes');
    	}
    	else{
    		jQuery('body').removeClass('larger');
    		jQuery.cookie("boxed",'no', {expires: 365, path: '/'});
    		//setBoxedCookies('no');
    	}
    	
    	
    }
    
    function updateSlider(){
    	
    	var slyder_type  = jQuery('#slider_type').val();
        

        
        jQuery.post( AJAXURL ,
                {
                    "action" : 'update_slider' ,
                    "slyder_type" : slyder_type
                    
                } ,
                function( data ){
                if(data == 'true'){
                	window.location.reload(true);
                }
                    
        });
    }
