$(document).ready(function() {
	
	$('#first_right').cycle({ 
	    fx:     'fade', 
	    speed:  'slow', 
	    timeout: 6000 
	});
	
	$('#newsline .nwrap span').cycle({ 
	    fx:     'fade', 
	    speed:  'slow', 
	    timeout: 4000 
	});
	/*
	$('header nav li') .hover(function() {
		var submenu = $(this).find('ul');
		if (submenu.size() > 0) {
			$(this).addClass('hoversub')
			submenu.slideDown(350);
		};
		
	}, function() {
		var submenu = $(this).find('ul');
		$(this).removeClass('hoversub')
		submenu.slideUp(200);
	});
	*/
	jQuery(document).ready(function(){ 
		jQuery('nav ul').superfish();
	});  
	$('a.fancybox').fancybox({
		'overlayShow' : true
	});
	$('a.fancyboxvideo').fancybox({
		'overlayShow' : true,
		'type'		:	'ajax',
		'scrolling'	:	'no'
		/*ajax : {
		    type	: "POST",
		    data	: 'mydata=test'
		}*/
	});
	Demo.init();
	$('#gallery .items').cycle({ 
	    speed:  'slow', 
	    timeout: 0, 
	    pager:	'#gallery-pager',
	    next: 	'a.nav-next',
	    prev:	'a.nav-prev',
	     
	    // callback fn that creates a thumbnail to use as pager anchor 
	    pagerAnchorBuilder: function(idx, slide) { 
	        return '<li><a href="javascript:void(0);" style="width:115px;height:77px;display:block;background:url(\'' + $(slide).attr('data') + '\') no-repeat center center;"></a></li>'; 
	    } 
	});
	
	$('form#order_form .half label') .click(function() {
		var label_txt = $(this).find("input").attr('data-label');
		var label = $('form#order_form label[for="input_name"]');
		$(label).text(label_txt);
		if ($(this).find("input").attr('data-company') == 1) {
			$('form#order_form .isCompany').slideDown("slow");
		}else{
			$('form#order_form .isCompany').slideUp("slow");
		}
	});
	
	$('form#order_form .row label:not("form#order_form .half label")').each(function(){
		var labelColor = '#999';
		var restingPosition = '5px';
	
		// style the label with JS for progressive enhancement
		$(this).css({
			'position' : 'absolute',
		 	'top' : '6px',
			'left' : restingPosition,
			'display' : 'inline',
	    	'z-index' : '99'
		});
	
		// grab the input value
		var inputval = $(this).next('input').val();
	
		// grab the label width, then add 5 pixels to it
		var labelwidth = $(this).width();
		var labelmove = labelwidth + 5;
	
		//onload, check if a field is filled out, if so, move the label out of the way
		if(inputval !== ''){
			$(this).stop().animate({ 'left':'-'+labelmove }, 1);
		}    	
	
		// if the input is empty on focus move the label to the left
		// if it's empty on blur, move it back
		$('input').focus(function(){
			var label = $(this).prev('label');
			var width = $(label).width();
			var adjust = width + 5;
			var value = $(this).val();
	
			if(value == ''){
				label.stop().animate({ 'left':'-'+adjust }, 'fast');
			} else {
				label.css({ 'left':'-'+adjust });
			}
		}).blur(function(){
			var label = $(this).prev('label');
			var value = $(this).val();
			if(value == ''){
				label.stop().animate({ 'left':restingPosition }, 'fast');
			}
		});
	});
	
	
	
	
});
	
	
function clearInput (selector) {
	var tags_input = $(selector).val();
	$(selector).click(function() {
		if ($(this).val() == tags_input) {
			$(this).val("").removeClass('focus');
		};
	});
}

var oldWrap = new Object(); 

function storePosition(theWrapper) { 
    var tmp = document.getElementById(theWrapper);
        if (tmp) {
            oldWrap.left = parseInt(tmp.style.left); 
            oldWrap.top = parseInt(tmp.style.top);   
        }
}


function storeSize(theWrapper) { 
    var tmp = document.getElementById(theWrapper);
        if (tmp) { 
            oldWrap.width = parseInt(tmp.style.width); 
            oldWrap.height = parseInt(tmp.style.height);
        }
}


function storeWrap(theWrapper) { storeSize(theWrapper); storePosition(theWrapper); }


function positionWrap(theWrapper, theLeft, theTop, unitStr) { 
    var tmp = document.getElementById(theWrapper);
        if (tmp) { 
            if (! unitStr) { var unitStr = 'px'; }
            tmp.style.left = parseInt(theLeft)+unitStr; 
tmp.style.top = parseInt(theTop)+unitStr;
        }
}


function sizeWrap(theWrapper, theWidth, theHeight, unitStr) { 
    var tmp = document.getElementById(theWrapper);
        if (tmp) { 
            if (! unitStr) { var unitStr = 'px'; }
            tmp.style.width = parseInt(theWidth)+unitStr; 
            tmp.style.height = parseInt(theHeight)+unitStr; 
        }
}


function showWrap(theWrapper, theLeft, theTop, theWidth, theHeight, unitStr) { 
            if (! unitStr) { var unitStr = 'px'; }
            positionWrap(theWrapper, theLeft, theTop, unitStr);
            sizeWrap(theWrapper, theWidth, theHeight, unitStr);
}
function createPlayer(theFile) {
    var flashvars = {
    	file:theFile, 
    	autostart:"true",
    	controlbar:"bottom",
    	type : 'sound',
    	duration: 600,
    	width: '200',
		height: '25'
    }
    var params = {
    	allowfullscreen:"false", 
    	allowscriptaccess:"always"
    	
    }
    var attributes = {
    	id:"player",  
    	name:"player"
    }
    swfobject.embedSWF("/static/js/player/player.swf", "player", "100%", "100%", "9.0.115", false, flashvars, params, attributes);
}


function hideWrap(theWrapper) { storeWrap(theWrapper);  showWrap(theWrapper, 0, 0, 0, 0); }

var FAQ = {

	go: function(dir, lang){
		id = $('aside .faq .title').attr('data-id');
		data = {
			a: 'faq',
			id: id,
			dir: dir,
			lang: lang
		}		
		
		$.ajax({
			url: "/si/ajax/",
			type: "POST",
			dataType: "json",
			data: data,
		
			success: function(r) {
				var box = $('aside .faq');
				$(box).find(".title").html(r.title).attr('data-id',r.id);
				$(box).find(".cont").html(r.content);
			}
		});
		
	}
}
function removePlayer() {
	$("#player").remove();
}
var Demo = {
	wrap: "#demounit",
	back: null,
	currentrow: null,
	currenttemplate: null,

		  
	init: function(){
		if ($(this.wrap) != undefined) {
			$(this.wrap+" .wrap ul").attr('class','main').html($(this.wrap).find('.templates ul.main').html());
			this.back = 'main';
			this.currenttemplate = 'main';
		};
		$(this.wrap+' ul li a').hover(function() {
			$(Demo.wrap+' ul li.current').removeClass('current');
			$(this).parent().addClass('current');
		}, function() {
		});
	},
	
	notAval: function(){
		$(Demo.wrap+' .wrap .notaval').fadeIn('100').delay(700).fadeOut();
	},
	
	next: function(){
		
	},
	
	prev: function(){
		if (this.back !== null) {
			this.goto(this.back);
		};
	},
	
	play: function(type,id){
		$(this.wrap+' .wrap ul li').removeClass('current');
		$(this.wrap+' .wrap ul li a[rel="'+id+'"]').parent().attr('class','current');
		var streamurl = $(this.wrap+' .wrap ul li a[rel="'+id+'"]').attr('data-stream');
		createPlayer(streamurl);
		showWrap('player', 100, 25, 100, 25);
    	storeWrap('player');
		hideWrap("player");
		var txt = $(this.wrap+' .wrap ul li a[rel="'+id+'"] span').text();
		$(this.wrap+' .wrap .nowplaying b').text(txt);
		$(this.wrap+' .wrap .nowplaying i.speaker').css({background: 'url("/static/images/play.gif") no-repeat'});
		
		
		setTimeout('removePlayer();',(1000*600));
	},
	
	goback: function(){
		if (this.back !== null) {
			this.goto(this.back);
		}else{
			this.goto('main');
		}
	},
	
	up: function(){
		var numitems = $(this.wrap+' .wrap ul li').size();
		var curritem = $(this.wrap+' .wrap ul li.current');
		var parent = $(this.wrap+' .wrap ul');
		var index = $(curritem).index();
		if ((index) <= 0) {
			index = numitems;
		};
		var nextitem =  $(this.wrap+' .wrap ul li:eq('+(index-1)+')');
		if (numitems > 5 && (numitems == (index))) {
			var nums = numitems - 5 - 1;
			var addtoback = $(this.wrap+' .wrap ul li:gt('+nums+')');
			$(this.wrap+' .wrap ul li:gt('+nums+')').remove();
			$(this.wrap+' .wrap ul').prepend(addtoback);
		}
		$(curritem).removeClass('current');
		$(nextitem).addClass('current');
		this.currentrow = index;
	},
	
	down: function(){
		var numitems = $(this.wrap+' .wrap ul li').size();
		var curritem = $(this.wrap+' .wrap ul li.current');
		var parent = $(this.wrap+' .wrap ul');
		var index = $(curritem).index();
		if ((index+1) >= numitems) {
			index = -1;
		};
		var nextitem =  $(this.wrap+' .wrap ul li:eq('+(index+1)+')');
		if (numitems > 5 && (index+1) >= 5) {
			
			var addtoback = $(this.wrap+' .wrap ul li:lt('+(index+1)+')');
			$(this.wrap+' .wrap ul li:lt('+(index+1)+')').remove();
			$(this.wrap+' .wrap ul').append(addtoback);
		}
		
		$(curritem).removeClass('current');
		$(nextitem).addClass('current');
		this.currentrow = index;
	},
	
	mid: function(){
		var curritem = $(this.wrap+' .wrap ul li.current a').attr('rel');
		if (!isNaN(curritem) && curritem != '' ) {
			Demo.play(Demo.currenttemplate,curritem);
		}else if(curritem != ''){
			Demo.goto(curritem);
		}else{
			Demo.notAval();
		}
	},
	
	goto: function(g){
		var back = $(this.wrap+" .wrap ul").attr('class');
		var template = $(this.wrap+' .templates ul.'+g).html();
		if (template !== null) {
			this.back = back;
			$(this.wrap+" .wrap ul").attr('class',g).html(template);
			$(this.wrap+" .wrap ul."+g+" li:eq(0)").addClass('current');
			this.currenttemplate = g;
			
			$(this.wrap+' .topbar').text($(this.wrap+' .templates ul.'+g).attr('data-title'));
			$(this.wrap+' ul li a').hover(function() {
				$(Demo.wrap+' ul li.current').removeClass('current');
				$(this).parent().addClass('current');
			}, function() { });	
		}
		
	}
	
}


        $(document).ready(function() {
            $(".dropdown img.flag").addClass("flagvisibility");

            $(".dropdown dt a").click(function() {
                $(".dropdown dd ul").toggle();
            });
                        
            $(".dropdown dd ul li a").click(function() {
                var text = $(this).html();
                $(".dropdown dt a span").html(text);
                $(".dropdown dd ul").hide();
                //$("#result").html("Selected value is: " + getSelectedValue("sample"));
            });
                        
            function getSelectedValue(id) {
                return $("#" + id).find("dt a span.value").html();
            }

            $(document).bind('click', function(e) {
                var $clicked = $(e.target);
                if (! $clicked.parents().hasClass("dropdown"))
                    $(".dropdown dd ul").hide();
            });


            
                $(".dropdown img.flag").toggleClass("flagvisibility");
            
        });

