// fixes $ / mrm.$ jQuery conflict call between site and gmds app
var mrm = mrm || {}; mrm.$ = mrm.$ || jQuery;

// override if console unavailable.
if (typeof console == "undefined") {
	window.console = {
		log: function () {}
	};
}

function locationWrapper( url ){
	if ( $.browser.msie ) {
		var referLink = document.createElement('a');
		referLink.href = url;
		document.body.appendChild(referLink);
		referLink.click();
	} else {
		window.location = url;
	}
}

function addLowerLine(){
	if( $( '#mds-app-dl div#criterial_filter_area .lowerMid' ).length > 0 ){
		$( '<div class="lowerLine"></div>' ).appendTo( '#mds-app-dl div#criterial_filter_area .lowerMid' );
	} else {
		setTimeout(
			function(){
				addLowerLine();
			},
			100
		);
	}
}
function updateOnOrientationChange(){
	switch( window.orientation ){
		case 0:
		case 180:
			//alert( 'Portrait' );
			//iPad and iPhone Portrait
			//Galaxy tab Landscape
			switch( true ){
				case navigator.platform == 'iPad':
					$( 'meta[name="viewport"]' ).attr( 'content', 'width = 980, initial-scale = .78, minimum-scale = .5, maximum-scale = 3' );
					break;
				case navigator.platform == 'iPhone':
					$( 'meta[name="viewport"]' ).attr( 'content', 'width = 980, initial-scale = 1, minimum-scale = .5, maximum-scale = 3' );
					break;
				case $( window ).width() == 1280 && navigator.userAgent.indexOf( 'Android' ) > -1:
					//android table landscape
					//alert( 'android tablet1' );
					$( 'meta[name="viewport"]' ).attr( 'content', 'width = device-width, initial-scale = 1, minimum-scale = .5, maximum-scale = 3' );
					break;
			}
			break;
		default:
			//alert( 'Landscape' );
			//iPad and iPhone Landscape
			//Galaxy tab Portrait
			switch( true ){
				case $( 'body' ).hasClass( 'homepage' ):
					$( 'meta[name="viewport"]' ).attr( 'content', 'width = 1024;, initial-scale = 1, minimum-scale = .5, maximum-scale = 3' );
					break;
				case $( window ).width() == 800 && navigator.userAgent.indexOf( 'Android' ) > -1:
					//alert( 'android tablet2' );
					$( 'meta[name="viewport"]' ).attr( 'content', 'width = 980, initial-scale = .78, minimum-scale = .5, maximum-scale = 3' );
					break;
				default:
					$( 'meta[name="viewport"]' ).attr( 'content', 'width = device-width, initial-scale = 1, minimum-scale = .5, maximum-scale = 3' );
			}
	}
}
updateOnOrientationChange();
$(document).ready( function(){
//	alert( 
//		"navigator.platform " + navigator.platform + "\n" +
//		"navigator.userAgent " + navigator.userAgent + "\n" +
//		"window height " + $( window ).height() + "\n" +
//		"body height " + $( 'body' ).height() + "\n" +
//		"window width " + $( window ).width() + "\n" +
//		"body width " + $( 'body' ).width() + "\n"
//	);
	if( navigator.platform == 'iPad' || navigator.platform == 'iPhone' ){
		$( 'body' ).resize( function(){ updateOnOrientationChange(); });
		updateOnOrientationChange();
	}
	$( 'div#primaryNavigation > ul' ).fill({
		elements : 'li',
		fill     : 'li#blackFill'
	});

	$( 'li.topNav-left' ).primaryNavigationSlideDown(); 

	$( 'li#vehicles, li#vision, li#company' ).primaryNavigationSlideDown();

	$( 'li#GMLogo' ).click( function(){
		location.href = "/content/saturn/northamerica/usa/nscwebsite/en/home.html";
	});

	$('ul #dealer_locator a').each( function(){
		if( typeof( Cufon ) == 'function' && Cufon.replace ){
			Cufon.replace( $( this ), { fontFamily: 'TradeGothic', hover: true });
		}
	});
	$('ul #contact_us a').each( function(){
		if( typeof( Cufon ) == 'function' && Cufon.replace ){
			Cufon.replace( $( this ), { fontFamily: 'TradeGothic', hover: true });
		}
	});

	$( '#primaryNavigation li form input' ).focus( function(){
		if( $( this ).attr( 'value' ) == 'SEARCH' )
			$( this ).attr( 'value', '' )
	}).blur( function(){
		if( $( this ).attr( 'value' ).length <= 0 )
			$( this ).attr( 'value', 'SEARCH' );
	});
	addLowerLine();
});
